<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="laravel-dusk" tabindex="-1"><a class="header-anchor" href="#laravel-dusk"><span>Laravel Dusk</span></a></h2>
<ul>
<li><a href="#introduction">介绍</a></li>
<li><a href="#installation">安装</a>
<ul>
<li><a href="#managing-chromedriver-installations">管理 ChromeDriver 安装</a></li>
<li><a href="#using-other-browsers">使用其他浏览器</a></li>
</ul>
</li>
<li><a href="#getting-started">入门指南</a>
<ul>
<li><a href="#generating-tests">生成测试</a></li>
<li><a href="#resetting-the-database-after-each-test">每次测试后重置数据库</a></li>
<li><a href="#running-tests">运行测试</a></li>
<li><a href="#environment-handling">环境处理</a></li>
</ul>
</li>
<li><a href="#browser-basics">浏览器基础</a>
<ul>
<li><a href="#creating-browsers">创建浏览器</a></li>
<li><a href="#navigation">导航</a></li>
<li><a href="#resizing-browser-windows">调整浏览器窗口大小</a></li>
<li><a href="#browser-macros">浏览器宏</a></li>
<li><a href="#authentication">身份认证</a></li>
<li><a href="#cookies">Cookies</a></li>
<li><a href="#executing-javascript">执行 JavaScript</a></li>
<li><a href="#taking-a-screenshot">截图</a></li>
<li><a href="#storing-console-output-to-disk">将控制台输出存储到磁盘</a></li>
<li><a href="#storing-page-source-to-disk">将页面源代码存储到磁盘</a></li>
</ul>
</li>
<li><a href="#interacting-with-elements">与元素交互</a>
<ul>
<li><a href="#dusk-selectors">Dusk 选择器</a></li>
<li><a href="#text-values-and-attributes">文本、值和属性</a></li>
<li><a href="#interacting-with-forms">与表单交互</a></li>
<li><a href="#attaching-files">附加文件</a></li>
<li><a href="#pressing-buttons">点击按钮</a></li>
<li><a href="#clicking-links">点击链接</a></li>
<li><a href="#using-the-keyboard">使用键盘</a></li>
<li><a href="#using-the-mouse">使用鼠标</a></li>
<li><a href="#javascript-dialogs">JavaScript 弹框</a></li>
<li><a href="#interacting-with-iframes">与内嵌窗口交互</a></li>
<li><a href="#scoping-selectors">范围选择器</a></li>
<li><a href="#waiting-for-elements">等待元素</a></li>
<li><a href="#scrolling-an-element-into-view">滚动元素到视图中</a></li>
</ul>
</li>
<li><a href="#available-assertions">可用的断言</a></li>
<li><a href="#pages">页面</a>
<ul>
<li><a href="#generating-pages">生成页面</a></li>
<li><a href="#configuring-pages">配置页面</a></li>
<li><a href="#navigating-to-pages">导航到页面</a></li>
<li><a href="#shorthand-selectors">快捷选择器</a></li>
<li><a href="#page-methods">页面方法</a></li>
</ul>
</li>
<li><a href="#components">组件</a>
<ul>
<li><a href="#generating-components">生成组件</a></li>
<li><a href="#using-components">使用组件</a></li>
</ul>
</li>
<li><a href="#continuous-integration">持续集成</a>
<ul>
<li><a href="#running-tests-on-heroku-ci">Heroku CI</a></li>
<li><a href="#running-tests-on-travis-ci">Travis CI</a></li>
<li><a href="#running-tests-on-github-actions">GitHub Actions</a></li>
<li><a href="#running-tests-on-chipper-ci">Chipper CI</a></li>
</ul>
</li>
</ul>
<h2 id="介绍" tabindex="-1"><a class="header-anchor" href="#介绍"><span>介绍</span></a></h2>
<p><a href="https://github.com/laravel/dusk" target="_blank" rel="noopener noreferrer">Laravel Dusk</a> 提供了一种表达性强、易于使用的浏览器自动化和测试 API。默认情况下，Dusk 不需要你在本地计算机上安装 JDK 或 Selenium。相反，Dusk 使用独立的 <a href="https://sites.google.com/chromium.org/driver" target="_blank" rel="noopener noreferrer">ChromeDriver</a> 安装。不过，你可以随便使用任何与 Selenium 兼容的驱动程序。</p>
<h2 id="安装" tabindex="-1"><a class="header-anchor" href="#安装"><span>安装</span></a></h2>
<p>要开始使用，你应该安装 <a href="https://www.google.com/chrome" target="_blank" rel="noopener noreferrer">Google Chrome</a> 并将 <code v-pre>laravel/dusk</code> Composer 依赖项添加到你的项目中：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> require laravel/dusk <span class="token parameter variable">--dev</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p>[!警告]<br>
如果你是手动注册 Dusk 的服务提供者，<strong>永远不要</strong> 在生产环境中注册它，因为这样做可能会导致任意用户能够通过你的应用程序进行认证。</p>
</blockquote>
<p>安装 Dusk 包后，执行 <code v-pre>dusk:install</code> Artisan 命令。<code v-pre>dusk:install</code> 命令将创建一个 <code v-pre>tests/Browser</code> 目录和一个示例 Dusk 测试，并为你当前的操作系统安装 Chrome Driver 二进制文件：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan dusk:install</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>接下来，在你的应用程序的 <code v-pre>.env</code> 文件中设置 <code v-pre>APP_URL</code> 环境变量。这个值应该与你通过浏览器访问应用程序时使用的 URL 一致。</p>
<blockquote>
<p>[!注意]<br>
如果你使用 <a href="https://learnku.com/docs/laravel/11.x/sail" target="_blank" rel="noopener noreferrer">Laravel Sail</a> 来管理你的本地开发环境，请同时查阅 Sail 文档中关于 <a href="https://learnku.com/docs/laravel/11.x/sail#laravel-dusk" target="_blank" rel="noopener noreferrer">配置和运行 Dusk 测试</a> 的部分。</p>
</blockquote>
<h3 id="管理-chromedriver-安装" tabindex="-1"><a class="header-anchor" href="#管理-chromedriver-安装"><span>管理 ChromeDriver 安装</span></a></h3>
<p>如果你想安装一个与 Laravel Dusk 通过 <code v-pre>dusk:install</code> 命令安装的版本不同的 ChromeDriver，你可以使用 <code v-pre>dusk:chrome-driver</code> 命令：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token comment"># 为你的操作系统安装最新版本的 ChromeDriver ...</span></span>
<span class="line">php artisan dusk:chrome-driver</span>
<span class="line"></span>
<span class="line"><span class="token comment"># 为你的操作系统安装指定版本的 ChromeDriver ...</span></span>
<span class="line">php artisan dusk:chrome-driver <span class="token number">86</span></span>
<span class="line"></span>
<span class="line"><span class="token comment"># 为所有支持的操作系统安装指定版本的 ChromeDriver ...</span></span>
<span class="line">php artisan dusk:chrome-driver <span class="token parameter variable">--all</span></span>
<span class="line"></span>
<span class="line"><span class="token comment"># 安装与你的操作系统检测到的 Chrome / Chromium 版本匹配的 ChromeDriver 版本...</span></span>
<span class="line">php artisan dusk:chrome-driver <span class="token parameter variable">--detect</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>[!警告]<br>
Dusk 需要 <code v-pre>chromedriver</code> 二进制文件是可执行的。如果你在运行 Dusk 时遇到问题，你应该使用以下命令确保二进制文件是可执行的：<code v-pre>chmod -R 0755 vendor/laravel/dusk/bin/</code>。</p>
</blockquote>
<h3 id="使用其他浏览器" tabindex="-1"><a class="header-anchor" href="#使用其他浏览器"><span>使用其他浏览器</span></a></h3>
<p>默认情况下，Dusk 使用 Google Chrome 和独立的 <a href="https://sites.google.com/chromium.org/driver" target="_blank" rel="noopener noreferrer">ChromeDriver</a> 安装来运行你的浏览器测试。不过，你可以启动自己的 Selenium 服务器，并针对任何你想要的浏览器运行测试。</p>
<p>要开始使用，请打开你的 <code v-pre>tests/DuskTestCase.php</code> 文件，这是你应用程序的基础 Dusk 测试用例。在这个文件中，你可以移除对 <code v-pre>startChromeDriver</code> 方法的调用。这将阻止 Dusk 自动启动 ChromeDriver：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 为 Dusk 测试执行做准备。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@beforeClass</span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">function</span> <span class="token function-definition function">prepare</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// static::startChromeDriver();</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>接下来，你可以修改 <code v-pre>driver</code> 方法以连接到你选择的 URL 和端口。此外，你还可以修改应该传递给 WebDriver 的「期望能力」：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Facebook<span class="token punctuation">\</span>WebDriver<span class="token punctuation">\</span>Remote<span class="token punctuation">\</span>RemoteWebDriver</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 创建 RemoteWebDriver 实例。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token keyword">function</span> <span class="token function-definition function">driver</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">RemoteWebDriver</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token class-name static-context">RemoteWebDriver</span><span class="token operator">::</span><span class="token function">create</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token string single-quoted-string">'http://localhost:4444/wd/hub'</span><span class="token punctuation">,</span> <span class="token class-name static-context">DesiredCapabilities</span><span class="token operator">::</span><span class="token function">phantomjs</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="入门指南" tabindex="-1"><a class="header-anchor" href="#入门指南"><span>入门指南</span></a></h2>
<h3 id="生成测试" tabindex="-1"><a class="header-anchor" href="#生成测试"><span>生成测试</span></a></h3>
<p>要生成一个 Dusk 测试，请使用 <code v-pre>dusk:make</code> Artisan 命令。生成的测试将放置在 <code v-pre>tests/Browser</code> 目录中：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan dusk:make LoginTest</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="每次测试后重置数据库" tabindex="-1"><a class="header-anchor" href="#每次测试后重置数据库"><span>每次测试后重置数据库</span></a></h3>
<p>你编写的大多数测试将与你带应用程序数据库检索数据的页面进行交互；但是，你的 Dusk 测试不应该使用 <code v-pre>RefreshDatabase</code> 特性。<code v-pre>RefreshDatabase</code> 特性利用了数据库事务，这些事务在跨 HTTP 请求时将不适用或不可用。然而，你有两个选择：<code v-pre>DatabaseMigrations</code> 特性和 <code v-pre>DatabaseTruncation</code> 特性。</p>
<h4 id="使用数据库迁移" tabindex="-1"><a class="header-anchor" href="#使用数据库迁移"><span>使用数据库迁移</span></a></h4>
<p><code v-pre>DatabaseMigrations</code> 特性将在每次测试之前运行你的数据库迁移。然而，对于每次测试，删除并重新创建数据库表通常比截断表要慢：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"><span class="token comment">// Pest</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Testing<span class="token punctuation">\</span>DatabaseMigrations</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Dusk<span class="token punctuation">\</span>Browser</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">uses</span><span class="token punctuation">(</span><span class="token class-name static-context">DatabaseMigrations</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">//</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"><span class="token comment">// PHPUnit</span></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">Tests<span class="token punctuation">\</span>Browser</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Testing<span class="token punctuation">\</span>DatabaseMigrations</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Dusk<span class="token punctuation">\</span>Browser</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Tests<span class="token punctuation">\</span>DuskTestCase</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">ExampleTest</span> <span class="token keyword">extends</span> <span class="token class-name">DuskTestCase</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">DatabaseMigrations</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">//</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>[!警告]<br>
在执行 Dusk 测试时，SQLite 内存数据库可能不被使用。因为浏览器在其自己的进程中执行，它将无法访问其他进程的内存数据库。</p>
</blockquote>
<h4 id="使用数据库截断" tabindex="-1"><a class="header-anchor" href="#使用数据库截断"><span>使用数据库截断</span></a></h4>
<p><code v-pre>DatabaseTruncation</code> 特性将在第一个测试中迁移你的数据库，以确保你的数据库表已正确创建。然而，在后续测试中，数据库的表将被简单地截断——与重新运行所有数据库迁移相比，提供了速度提升：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"><span class="token comment">// Pest</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Testing<span class="token punctuation">\</span>DatabaseTruncation</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Dusk<span class="token punctuation">\</span>Browser</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">uses</span><span class="token punctuation">(</span><span class="token class-name static-context">DatabaseTruncation</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">//</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"><span class="token comment">// PHPUnit</span></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">Tests<span class="token punctuation">\</span>Browser</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Testing<span class="token punctuation">\</span>DatabaseTruncation</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Dusk<span class="token punctuation">\</span>Browser</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Tests<span class="token punctuation">\</span>DuskTestCase</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">ExampleTest</span> <span class="token keyword">extends</span> <span class="token class-name">DuskTestCase</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">DatabaseTruncation</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">//</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>默认情况下，此特性将截断除 <code v-pre>migrations</code> 表之外的所有表。如果你想自定义应该截断的表，可以在你的测试类上定义一个 <code v-pre>$tablesToTruncate</code> 属性：</p>
<blockquote>
<p>[!注意]<br>
如果你使用 Pest，你应该在基础 <code v-pre>DuskTestCase</code> 类或你的测试文件扩展的任何类上定义属性或方法。</p>
</blockquote>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 指示应该截断哪些表</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@var</span> <span class="token class-name"><span class="token keyword">array</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token variable">$tablesToTruncate</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>或者，你可以在你的测试类上定义一个 <code v-pre>$exceptTables</code> 属性，以指定哪些表不被截断：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 指示应该从截断中排除哪些表。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@var</span> <span class="token class-name"><span class="token keyword">array</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token variable">$exceptTables</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>要指定应该截断表所在的数据库连接，可以在你的测试类上定义一个 <code v-pre>$connectionsToTruncate</code> 属性：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 指示应该截断哪些数据库连接的表。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@var</span> <span class="token class-name"><span class="token keyword">array</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token variable">$connectionsToTruncate</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'mysql'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你想在执行数据库截断之前或之后执行代码，可以在你的测试类上定义 <code v-pre>beforeTruncatingDatabase</code> 或 <code v-pre>afterTruncatingDatabase</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 在数据库开始截断之前执行任何应该完成的工作。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token keyword">function</span> <span class="token function-definition function">beforeTruncatingDatabase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">//</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 在数据库完成截断之后执行任何应该完成的工作。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token keyword">function</span> <span class="token function-definition function">afterTruncatingDatabase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">//</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="运行测试" tabindex="-1"><a class="header-anchor" href="#运行测试"><span>运行测试</span></a></h3>
<p>要运行你的浏览器测试，执行 <code v-pre>dusk</code> Artisan 命令：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan dusk</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果你上次运行 <code v-pre>dusk</code> 命令时出现了测试失败，你可以使用 <code v-pre>dusk:fails</code> 命令先重新运行失败的测试，以节省时间：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan dusk:fails</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><code v-pre>dusk</code> 命令接受 Pest / PHPUnit 测试运行器正常接受的任何参数，例如允许你只为给定的 <a href="https://docs.phpunit.de/en/10.5/annotations.html#group" target="_blank" rel="noopener noreferrer">组</a> 运行测试：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan dusk <span class="token parameter variable">--group</span><span class="token operator">=</span>foo</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p>[!注意]<br>
如果你使用 <a href="https://learnku.com/docs/laravel/11.x/sail" target="_blank" rel="noopener noreferrer">Laravel Sail</a> 来管理你的本地开发环境，请查阅 Sail 文档中关于 <a href="https://learnku.com/docs/laravel/11.x/sail#laravel-dusk" target="_blank" rel="noopener noreferrer">配置和运行 Dusk 测试</a> 的部分。</p>
</blockquote>
<h4 id="手动启动-chromedriver" tabindex="-1"><a class="header-anchor" href="#手动启动-chromedriver"><span>手动启动 ChromeDriver</span></a></h4>
<p>默认情况下，Dusk 将自动尝试启动 ChromeDriver。如果这对你的特定系统不起作用，你可以在运行 <code v-pre>dusk</code> 命令之前手动启动 ChromeDriver。如果你选择手动启动 ChromeDriver，你应该注释掉 <code v-pre>tests/DuskTestCase.php</code> 文件中的以下行：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 为 Dusk 测试执行做准备。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@beforeClass</span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">function</span> <span class="token function-definition function">prepare</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// static::startChromeDriver();</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>此外，如果你在 9515 以外的端口上启动 ChromeDriver，你应该修改同一个类的 <code v-pre>driver</code> 方法来表明正确的端口：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Facebook<span class="token punctuation">\</span>WebDriver<span class="token punctuation">\</span>Remote<span class="token punctuation">\</span>RemoteWebDriver</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 创建 RemoteWebDriver 实例。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token keyword">function</span> <span class="token function-definition function">driver</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">RemoteWebDriver</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token class-name static-context">RemoteWebDriver</span><span class="token operator">::</span><span class="token function">create</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token string single-quoted-string">'http://localhost:9515'</span><span class="token punctuation">,</span> <span class="token class-name static-context">DesiredCapabilities</span><span class="token operator">::</span><span class="token function">chrome</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="环境处理" tabindex="-1"><a class="header-anchor" href="#环境处理"><span>环境处理</span></a></h3>
<p>要强制 Dusk 在运行测试时使用其自己的环境文件，请在你的项目根目录中创建一个 <code v-pre>.env.dusk.{environment}</code> 文件。例如，如果你将从 <code v-pre>local</code> 环境启动 <code v-pre>dusk</code> 命令，你应该创建一个 <code v-pre>.env.dusk.local</code> 文件。</p>
<p>运行测试时，Dusk 将备份你的 <code v-pre>.env</code> 文件，并将你的 Dusk 环境重命名为 <code v-pre>.env</code>。测试完成后，你的 <code v-pre>.env</code> 文件将被恢复。</p>
<h2 id="浏览器基础" tabindex="-1"><a class="header-anchor" href="#浏览器基础"><span>浏览器基础</span></a></h2>
<h3 id="创建浏览器" tabindex="-1"><a class="header-anchor" href="#创建浏览器"><span>创建浏览器</span></a></h3>
<p>要开始使用，让我们编写一个测试，验证我们可以登录到我们的应用程序。生成测试后，我们可以修改它导航到登录页面，输入一些凭证，并点击「登录」按钮。要创建一个浏览器实例，你可以从你的 Dusk 测试中调用 <code v-pre>browse</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"><span class="token comment">// Pest</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Testing<span class="token punctuation">\</span>DatabaseMigrations</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Dusk<span class="token punctuation">\</span>Browser</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">uses</span><span class="token punctuation">(</span><span class="token class-name static-context">DatabaseMigrations</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">test</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'basic example'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$user</span> <span class="token operator">=</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">factory</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">create</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'taylor@laravel.com'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">browse</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Browser</span> <span class="token variable">$browser</span><span class="token punctuation">)</span> <span class="token keyword">use</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">visit</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/login'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">type</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">,</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">email</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">type</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'password'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'password'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">press</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Login'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">assertPathIs</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/home'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"><span class="token comment">// PHPUnit</span></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">Tests<span class="token punctuation">\</span>Browser</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Testing<span class="token punctuation">\</span>DatabaseMigrations</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Dusk<span class="token punctuation">\</span>Browser</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Tests<span class="token punctuation">\</span>DuskTestCase</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">ExampleTest</span> <span class="token keyword">extends</span> <span class="token class-name">DuskTestCase</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">DatabaseMigrations</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 一个基本的浏览器测试示例。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">test_basic_example</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$user</span> <span class="token operator">=</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">factory</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">create</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'taylor@laravel.com'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">browse</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Browser</span> <span class="token variable">$browser</span><span class="token punctuation">)</span> <span class="token keyword">use</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">visit</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/login'</span><span class="token punctuation">)</span></span>
<span class="line">                    <span class="token operator">-></span><span class="token function">type</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">,</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">email</span><span class="token punctuation">)</span></span>
<span class="line">                    <span class="token operator">-></span><span class="token function">type</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'password'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'password'</span><span class="token punctuation">)</span></span>
<span class="line">                    <span class="token operator">-></span><span class="token function">press</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Login'</span><span class="token punctuation">)</span></span>
<span class="line">                    <span class="token operator">-></span><span class="token function">assertPathIs</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/home'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如上例所示，<code v-pre>browse</code> 方法接受一个闭包。由 Dusk 自动将浏览器实例传递给这个闭包，这是用于与你的应用程序交互和进行断言的主要对象。</p>
<h4 id="创建多个浏览器" tabindex="-1"><a class="header-anchor" href="#创建多个浏览器"><span>创建多个浏览器</span></a></h4>
<p>有时你可能需要多个浏览器才能正确执行测试。例如，测试与 WebSocket 交互的聊天屏幕可能需要多个浏览器。要创建多个浏览器，只需在给 <code v-pre>browse</code> 方法的闭包签名中添加更多浏览器参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">browse</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Browser</span> <span class="token variable">$first</span><span class="token punctuation">,</span> <span class="token class-name type-declaration">Browser</span> <span class="token variable">$second</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$first</span><span class="token operator">-></span><span class="token function">loginAs</span><span class="token punctuation">(</span><span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">          <span class="token operator">-></span><span class="token function">visit</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/home'</span><span class="token punctuation">)</span></span>
<span class="line">          <span class="token operator">-></span><span class="token function">waitForText</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Message'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$second</span><span class="token operator">-></span><span class="token function">loginAs</span><span class="token punctuation">(</span><span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">           <span class="token operator">-></span><span class="token function">visit</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/home'</span><span class="token punctuation">)</span></span>
<span class="line">           <span class="token operator">-></span><span class="token function">waitForText</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Message'</span><span class="token punctuation">)</span></span>
<span class="line">           <span class="token operator">-></span><span class="token function">type</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'message'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Hey Taylor'</span><span class="token punctuation">)</span></span>
<span class="line">           <span class="token operator">-></span><span class="token function">press</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Send'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$first</span><span class="token operator">-></span><span class="token function">waitForText</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hey Taylor'</span><span class="token punctuation">)</span></span>
<span class="line">          <span class="token operator">-></span><span class="token function">assertSee</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Jeffrey Way'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="导航" tabindex="-1"><a class="header-anchor" href="#导航"><span>导航</span></a></h3>
<p><code v-pre>visit</code> 方法可用于在你的应用程序中导航到给定的 URI：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">visit</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/login'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你可以使用 <code v-pre>visitRoute</code> 方法导航到 <a href="https://learnku.com/docs/laravel/11.x/routing#named-routes" target="_blank" rel="noopener noreferrer">命名路由</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">visitRoute</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'login'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你可以使用 <code v-pre>back</code> 和 <code v-pre>forward</code> 方法进行「后退」和「前进」导航：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">back</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">forward</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以使用 <code v-pre>refresh</code> 方法刷新页面：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">refresh</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="调整浏览器窗口大小" tabindex="-1"><a class="header-anchor" href="#调整浏览器窗口大小"><span>调整浏览器窗口大小</span></a></h3>
<p>你可以使用 <code v-pre>resize</code> 方法调整浏览器窗口的大小：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">resize</span><span class="token punctuation">(</span><span class="token number">1920</span><span class="token punctuation">,</span> <span class="token number">1080</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><code v-pre>maximize</code> 方法可用于最大化浏览器窗口：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">maximize</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><code v-pre>fitContent</code> 方法将调整浏览器窗口的大小以适应其内容的大小：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">fitContent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>当测试失败时，Dusk 会自动调整浏览器窗口的大小以适应内容，然后获取一个截图。你可以在测试中调用 <code v-pre>disableFitOnFailure</code> 方法来禁用此功能：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">disableFitOnFailure</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你可以使用 <code v-pre>move</code> 方法将浏览器窗口移动到屏幕上的不同位置：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">move</span><span class="token punctuation">(</span><span class="token variable">$x</span> <span class="token operator">=</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token variable">$y</span> <span class="token operator">=</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="浏览器宏" tabindex="-1"><a class="header-anchor" href="#浏览器宏"><span>浏览器宏</span></a></h3>
<p>如果你想定义一个可以在多个测试中重用的自定义浏览器方法，你可以在 <code v-pre>Browser</code> 类上使用 <code v-pre>macro</code> 方法。通常，你应该从 <a href="https://learnku.com/docs/laravel/11.x/providers" target="_blank" rel="noopener noreferrer">服务提供者</a> 的 <code v-pre>boot</code> 方法中调用此方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Providers</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>ServiceProvider</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Dusk<span class="token punctuation">\</span>Browser</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">DuskServiceProvider</span> <span class="token keyword">extends</span> <span class="token class-name">ServiceProvider</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 注册 Dusk 的浏览器宏。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token class-name static-context">Browser</span><span class="token operator">::</span><span class="token function">macro</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'scrollToElement'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$element</span> <span class="token operator">=</span> <span class="token constant">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">script</span><span class="token punctuation">(</span><span class="token string double-quoted-string">"$('html, body').animate({ scrollTop: $('<span class="token interpolation"><span class="token variable">$element</span></span>').offset().top }, 0);"</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">            <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>macro</code> 函数接受一个名称作为其第一个参数，一个闭包作为其第二个参数。宏的闭包将在 <code v-pre>Browser</code> 实例上调用宏方法时执行：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">browse</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Browser</span> <span class="token variable">$browser</span><span class="token punctuation">)</span> <span class="token keyword">use</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">visit</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/pay'</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">scrollToElement</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'#credit-card-details'</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">assertSee</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Enter Credit Card Details'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="身份认证" tabindex="-1"><a class="header-anchor" href="#身份认证"><span>身份认证</span></a></h3>
<p>你经常会测试需要认证的页面。你可以使用 Dusk 的 <code v-pre>loginAs</code> 方法来避免在每次测试中与应用程序的登录界面交互。<code v-pre>loginAs</code> 方法接受与你的可认证模型关联的主键或一个可认证模型实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Dusk<span class="token punctuation">\</span>Browser</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">browse</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Browser</span> <span class="token variable">$browser</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">loginAs</span><span class="token punctuation">(</span><span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">          <span class="token operator">-></span><span class="token function">visit</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/home'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>[!警告]<br>
使用 <code v-pre>loginAs</code> 方法后，用户会话（session）将在该文件中的所有测试中保持。</p>
</blockquote>
<h3 id="cookies" tabindex="-1"><a class="header-anchor" href="#cookies"><span>Cookies</span></a></h3>
<p>你可以使用 <code v-pre>cookie</code> 方法来获取或设置一个加密 cookie 的值。默认情况下，Laravel 创建的所有 cookie 都是加密的：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">cookie</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">cookie</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以使用 <code v-pre>plainCookie</code> 方法来获取或设置一个未加密 cookie 的值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">plainCookie</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">plainCookie</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以使用 <code v-pre>deleteCookie</code> 方法来删除指定的 cookie：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">deleteCookie</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="执行-javascript" tabindex="-1"><a class="header-anchor" href="#执行-javascript"><span>执行 JavaScript</span></a></h3>
<p>你可以使用 <code v-pre>script</code> 方法在浏览器中执行任意的 JavaScript 语句：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">script</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'document.documentElement.scrollTop = 0'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">script</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'document.body.scrollTop = 0'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'document.documentElement.scrollTop = 0'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$output</span> <span class="token operator">=</span> <span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">script</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'return window.location.pathname'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="截图" tabindex="-1"><a class="header-anchor" href="#截图"><span>截图</span></a></h3>
<p>你可以使用 <code v-pre>screenshot</code> 方法来获取屏幕截图并使用指定的文件名存储。所有截图将存储在 <code v-pre>tests/Browser/screenshots</code> 目录中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">screenshot</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'filename'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><code v-pre>responsiveScreenshots</code> 方法可用于在各种断点处截取一系列屏幕截图：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">responsiveScreenshots</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'filename'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><code v-pre>screenshotElement</code> 方法可用于获取页面上特定元素的屏幕截图：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">screenshotElement</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'#selector'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'filename'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="将控制台输出存储到磁盘" tabindex="-1"><a class="header-anchor" href="#将控制台输出存储到磁盘"><span>将控制台输出存储到磁盘</span></a></h3>
<p>你可以使用 <code v-pre>storeConsoleLog</code> 方法将当前浏览器的控制台输出以给定的文件名写入磁盘。控制台输出将存储在 <code v-pre>tests/Browser/console</code> 目录中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">storeConsoleLog</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'filename'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="将页面源代码存储到磁盘" tabindex="-1"><a class="header-anchor" href="#将页面源代码存储到磁盘"><span>将页面源代码存储到磁盘</span></a></h3>
<p>你可以使用 <code v-pre>storeSource</code> 方法将当前页面的源代码以给定的文件名写入磁盘。页面源代码将存储在 <code v-pre>tests/Browser/source</code> 目录中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">storeSource</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'filename'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="与元素交互" tabindex="-1"><a class="header-anchor" href="#与元素交互"><span>与元素交互</span></a></h2>
<h3 id="dusk-选择器" tabindex="-1"><a class="header-anchor" href="#dusk-选择器"><span>Dusk 选择器</span></a></h3>
<p>选择好的 CSS 选择器来与元素交互是编写 Dusk 测试中最困难的部分之一。随着时间的推移，前端的变化可能导致以下这样的 CSS 选择器破坏你的测试：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// HTML...</span></span>
<span class="line"></span>
<span class="line"><span class="token operator">&lt;</span>button<span class="token operator">></span>Login<span class="token operator">&lt;</span><span class="token operator">/</span>button<span class="token operator">></span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Test...</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">click</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'.login-page .container div > button'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Dusk 选择器允许你专注于编写有效的测试，而不是记住 CSS 选择器。要定义一个选择器，请在你的 HTML 元素中添加一个 <code v-pre>dusk</code> 属性。然后，在与 Dusk 浏览器交互时，在选择器前加上 <code v-pre>@</code> 来操作测试中的附加元素：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// HTML...</span></span>
<span class="line"></span>
<span class="line"><span class="token operator">&lt;</span>button dusk<span class="token operator">=</span><span class="token string double-quoted-string">"login-button"</span><span class="token operator">></span>Login<span class="token operator">&lt;</span><span class="token operator">/</span>button<span class="token operator">></span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Test...</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">click</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'@login-button'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果需要，你可以通过 <code v-pre>selectorHtmlAttribute</code> 方法自定义 Dusk 选择器使用的 HTML 属性。通常，这个方法应该从应用程序的 <code v-pre>AppServiceProvider</code> 的 <code v-pre>boot</code> 方法中调用：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Dusk<span class="token punctuation">\</span>Dusk</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Dusk</span><span class="token operator">::</span><span class="token function">selectorHtmlAttribute</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'data-dusk'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="文本、值和属性" tabindex="-1"><a class="header-anchor" href="#文本、值和属性"><span>文本、值和属性</span></a></h3>
<h4 id="检索和设置值" tabindex="-1"><a class="header-anchor" href="#检索和设置值"><span>检索和设置值</span></a></h4>
<p>Dusk 提供了多种方法来与页面上的元素的当前值、显示文本和属性进行交互。例如，要获取与给定 CSS 或 Dusk 选择器匹配的元素的「值」，请使用 <code v-pre>value</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 检索值...</span></span>
<span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">value</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'selector'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 设置值...</span></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">value</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'selector'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以使用 <code v-pre>inputValue</code> 方法来获取具有指定字段名称的输入元素的「值」：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">inputValue</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'field'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="检索文本" tabindex="-1"><a class="header-anchor" href="#检索文本"><span>检索文本</span></a></h4>
<p><code v-pre>text</code> 方法可用于检索与给定选择器匹配元素的显示文本：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$text</span> <span class="token operator">=</span> <span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">text</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'selector'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="检索属性" tabindex="-1"><a class="header-anchor" href="#检索属性"><span>检索属性</span></a></h4>
<p>最后，<code v-pre>attribute</code> 方法可用于检索与给定选择器匹配元素的属性的值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$attribute</span> <span class="token operator">=</span> <span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">attribute</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'selector'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="与表单交互" tabindex="-1"><a class="header-anchor" href="#与表单交互"><span>与表单交互</span></a></h3>
<h4 id="输入值" tabindex="-1"><a class="header-anchor" href="#输入值"><span>输入值</span></a></h4>
<p>Dusk 提供了多种方法来与表单和输入元素进行交互。首先，让我们看一个在输入字段中输入文本的例子：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">type</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'taylor@laravel.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>请注意，尽管该方法在必要时接受一个 CSS 选择器，但我们不需要在 <code v-pre>type</code> 方法中传递 CSS 选择器。如果没有提供 CSS 选择器，Dusk 将搜索具有给定 <code v-pre>name</code> 属性的 <code v-pre>input</code> 或 <code v-pre>textarea</code> 字段。</p>
<p>要在不清除其内容的情况下向字段追加文本，你可以使用 <code v-pre>append</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">type</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'tags'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'foo'</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">append</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'tags'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">', bar, baz'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以使用 <code v-pre>clear</code> 方法清除输入的值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你可以使用 <code v-pre>typeSlowly</code> 方法指示 Dusk 缓慢输入。默认情况下，Dusk 将在每次按键之间暂停 100 毫秒。要自定义按键之间的时间间隔，你可以将适当的毫秒数作为方法的第三个参数传递：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">typeSlowly</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mobile'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'+1 (202) 555-5555'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">typeSlowly</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mobile'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'+1 (202) 555-5555'</span><span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以使用 <code v-pre>appendSlowly</code> 方法缓慢追加文本：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">type</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'tags'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'foo'</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">appendSlowly</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'tags'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">', bar, baz'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="下拉选择" tabindex="-1"><a class="header-anchor" href="#下拉选择"><span>下拉选择</span></a></h4>
<p>要选择 <code v-pre>select</code> 元素上可用的值，你可以使用 <code v-pre>select</code> 方法。与 <code v-pre>type</code> 方法类似，<code v-pre>select</code> 方法不需要完整的 CSS 选择器。当向 <code v-pre>select</code> 方法传递值时，你应该传递选项的底层值而不是显示文本：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">select</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'size'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Large'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你可以通过省略第二个参数来选择一个随机选项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">select</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'size'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>通过向 <code v-pre>select</code> 方法的第二个参数提供一个数组，你可以指示该方法选择多个选项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">select</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'categories'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'Art'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Music'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="复选框" tabindex="-1"><a class="header-anchor" href="#复选框"><span>复选框</span></a></h4>
<p>要「勾选」一个复选框输入，你可以使用 <code v-pre>check</code> 方法。与其他许多与输入相关的方法类似，不需要完整的 CSS 选择器。如果找不到 CSS 选择器匹配，Dusk 将搜索具有匹配 <code v-pre>name</code> 属性的复选框：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">check</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'terms'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><code v-pre>uncheck</code> 方法可用于「取消勾选」一个复选框输入：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">uncheck</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'terms'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="单选按钮" tabindex="-1"><a class="header-anchor" href="#单选按钮"><span>单选按钮</span></a></h4>
<p>要「选择」一个 <code v-pre>radio</code> 输入选项，你可以使用 <code v-pre>radio</code> 方法。与其他许多与输入相关的方法类似，不需要完整的 CSS 选择器。如果找不到 CSS 选择器匹配，Dusk 将搜索具有匹配 <code v-pre>name</code> 和 <code v-pre>value</code> 属性的 <code v-pre>radio</code> 输入：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">radio</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'size'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'large'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="附加文件" tabindex="-1"><a class="header-anchor" href="#附加文件"><span>附加文件</span></a></h3>
<p><code v-pre>attach</code> 方法可用于将文件附加到 <code v-pre>file</code> 输入元素。与其他许多与输入相关的方法类似，不需要完整的 CSS 选择器。如果找不到 CSS 选择器匹配，Dusk 将搜索具有匹配 <code v-pre>name</code> 属性的 <code v-pre>file</code> 输入：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">attach</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'photo'</span><span class="token punctuation">,</span> <span class="token constant">__DIR__</span><span class="token operator">.</span><span class="token string single-quoted-string">'/photos/mountains.png'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p>[!警告]<br>
<code v-pre>attach</code> 函数要求在你的服务器上安装并启用 <code v-pre>Zip</code> PHP 扩展。</p>
</blockquote>
<h3 id="点击按钮" tabindex="-1"><a class="header-anchor" href="#点击按钮"><span>点击按钮</span></a></h3>
<p><code v-pre>press</code> 方法可用于点击页面上的按钮元素。传递给 <code v-pre>press</code> 方法的参数可以是按钮的显示文本或 CSS / Dusk 选择器：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">press</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Login'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>在提交表单时，许多应用程序在按下提交按钮后会禁用该按钮，然后在表单提交的 HTTP 请求完成后重新启用该按钮。要按下按钮并等待按钮被重新启用，你可以使用 <code v-pre>pressAndWaitFor</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 按下按钮并等待最多 5 秒钟以重新启用它...</span></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">pressAndWaitFor</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Save'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 按下按钮并等待最多 1 秒钟以重新启用它...</span></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">pressAndWaitFor</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Save'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="点击链接" tabindex="-1"><a class="header-anchor" href="#点击链接"><span>点击链接</span></a></h3>
<p>要点击一个链接，你可以在浏览器实例上使用 <code v-pre>clickLink</code> 方法。<code v-pre>clickLink</code> 方法将点击具有指定显示文本的链接：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">clickLink</span><span class="token punctuation">(</span><span class="token variable">$linkText</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你可以使用 <code v-pre>seeLink</code> 方法来确定页面上是否显示具有指定显示文本的链接：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">seeLink</span><span class="token punctuation">(</span><span class="token variable">$linkText</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>[!警告]<br>
这些方法与 jQuery 交互。如果页面上没有 jQuery，Dusk 将自动将其注入页面，以便在测试期间可用。</p>
</blockquote>
<h3 id="使用键盘" tabindex="-1"><a class="header-anchor" href="#使用键盘"><span>使用键盘</span></a></h3>
<p><code v-pre>keys</code> 方法允许你向指定元素提供比 <code v-pre>type</code> 方法通常允许的更复杂的输入序列。例如，你可以指示 Dusk 在输入值时按住修饰键。在这个例子中，在输入与指定选择器匹配的元素时，将按住 <code v-pre>shift</code> 键输入 <code v-pre>taylor</code>。输入 <code v-pre>taylor</code> 后，将不带任何修饰键输入 <code v-pre>swift</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">keys</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'selector'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'{shift}'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'taylor'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'swift'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><code v-pre>keys</code> 方法的另一个有价值的用例是向应用程序的主 CSS 选择器发送「键盘快捷键」组合：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">keys</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'.app'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'{command}'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'j'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p>[!注意]<br>
所有修饰键（如 <code v-pre>{command}</code>）都用 <code v-pre>{}</code> 字符包裹，并与 <code v-pre>Facebook\WebDriver\WebDriverKeys</code> 类中定义的常量匹配，可以在 <a href="https://github.com/php-webdriver/php-webdriver/blob/master/lib/WebDriverKeys.php" target="_blank" rel="noopener noreferrer">GitHub</a> 上找到。</p>
</blockquote>
<h4 id="流畅的键盘交互" tabindex="-1"><a class="header-anchor" href="#流畅的键盘交互"><span>流畅的键盘交互</span></a></h4>
<p>Dusk 还提供了一个 <code v-pre>withKeyboard</code> 方法，允许你通过 <code v-pre>Laravel\Dusk\Keyboard</code> 类流畅地执行复杂的键盘交互。<code v-pre>Keyboard</code> 类提供了 <code v-pre>press</code>、<code v-pre>release</code>、<code v-pre>type</code> 和 <code v-pre>pause</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Dusk<span class="token punctuation">\</span>Keyboard</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">withKeyboard</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Keyboard</span> <span class="token variable">$keyboard</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$keyboard</span><span class="token operator">-></span><span class="token function">press</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'c'</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">pause</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">release</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'c'</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">type</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'c'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'e'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'o'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="键盘宏" tabindex="-1"><a class="header-anchor" href="#键盘宏"><span>键盘宏</span></a></h4>
<p>如果你想定义可以在整个测试套件中容易重用的自定义键盘交互，你可以使用 <code v-pre>Keyboard</code> 类提供的 <code v-pre>macro</code> 方法。通常，你应该从 <a href="https://learnku.com/docs/laravel/11.x/providers" target="_blank" rel="noopener noreferrer">服务提供者</a> 的 <code v-pre>boot</code> 方法中调用此方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Providers</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Facebook<span class="token punctuation">\</span>WebDriver<span class="token punctuation">\</span>WebDriverKeys</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>ServiceProvider</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Dusk<span class="token punctuation">\</span>Keyboard</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Dusk<span class="token punctuation">\</span>OperatingSystem</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">DuskServiceProvider</span> <span class="token keyword">extends</span> <span class="token class-name">ServiceProvider</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 注册 Dusk 的浏览器宏。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token class-name static-context">Keyboard</span><span class="token operator">::</span><span class="token function">macro</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'copy'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$element</span> <span class="token operator">=</span> <span class="token constant">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">type</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">                <span class="token class-name static-context">OperatingSystem</span><span class="token operator">::</span><span class="token function">onMac</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token class-name static-context">WebDriverKeys</span><span class="token operator">::</span><span class="token constant">META</span> <span class="token punctuation">:</span> <span class="token class-name static-context">WebDriverKeys</span><span class="token operator">::</span><span class="token constant">CONTROL</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'c'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">            <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token class-name static-context">Keyboard</span><span class="token operator">::</span><span class="token function">macro</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'paste'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$element</span> <span class="token operator">=</span> <span class="token constant">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">type</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">                <span class="token class-name static-context">OperatingSystem</span><span class="token operator">::</span><span class="token function">onMac</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token class-name static-context">WebDriverKeys</span><span class="token operator">::</span><span class="token constant">META</span> <span class="token punctuation">:</span> <span class="token class-name static-context">WebDriverKeys</span><span class="token operator">::</span><span class="token constant">CONTROL</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'v'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">            <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>macro</code> 函数接受一个名称作为其第一个参数，一个闭包作为其第二个参数。宏的闭包将在 <code v-pre>Keyboard</code> 实例上调用宏方法时执行：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">click</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'@textarea'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">withKeyboard</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Keyboard</span> <span class="token variable">$keyboard</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token variable">$keyboard</span><span class="token operator">-></span><span class="token function">copy</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">click</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'@another-textarea'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">withKeyboard</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Keyboard</span> <span class="token variable">$keyboard</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token variable">$keyboard</span><span class="token operator">-></span><span class="token function">paste</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="使用鼠标" tabindex="-1"><a class="header-anchor" href="#使用鼠标"><span>使用鼠标</span></a></h3>
<h4 id="点击元素" tabindex="-1"><a class="header-anchor" href="#点击元素"><span>点击元素</span></a></h4>
<p><code v-pre>click</code> 方法可用于点击与给定 CSS 或 Dusk 选择器匹配的元素：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">click</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'.selector'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><code v-pre>clickAtXPath</code> 方法可用于点击与指定 XPath 表达式匹配的元素：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">clickAtXPath</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'//div[@class = "selector"]'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><code v-pre>clickAtPoint</code> 方法可用于点击位于指定一对坐标处的最顶层元素，这些坐标相对于浏览器的可视区域：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">clickAtPoint</span><span class="token punctuation">(</span><span class="token variable">$x</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token variable">$y</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><code v-pre>doubleClick</code> 方法可用于模拟鼠标的双击：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">doubleClick</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">doubleClick</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'.selector'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>rightClick</code> 方法可用于模拟鼠标的右键点击：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">rightClick</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">rightClick</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'.selector'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>clickAndHold</code> 方法可用于模拟鼠标按钮被点击并按住。随后调用 <code v-pre>releaseMouse</code> 方法将取消此行为并释放鼠标按钮：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">clickAndHold</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'.selector'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">clickAndHold</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">pause</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">releaseMouse</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>controlClick</code> 方法可用于模拟浏览器中的 <code v-pre>ctrl+click</code> 事件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">controlClick</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">controlClick</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'.selector'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="鼠标悬停" tabindex="-1"><a class="header-anchor" href="#鼠标悬停"><span>鼠标悬停</span></a></h4>
<p><code v-pre>mouseover</code> 方法可用于当你需要将鼠标移动到与指定 CSS 或 Dusk 选择器匹配的元素上时：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">mouseover</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'.selector'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="拖放" tabindex="-1"><a class="header-anchor" href="#拖放"><span>拖放</span></a></h4>
<p><code v-pre>drag</code> 方法可用于将匹配给定选择器的元素拖动到另一个元素上：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">drag</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'.from-selector'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'.to-selector'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>或者，你可以将元素沿单一方向拖动：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">dragLeft</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'.selector'</span><span class="token punctuation">,</span> <span class="token variable">$pixels</span> <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">dragRight</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'.selector'</span><span class="token punctuation">,</span> <span class="token variable">$pixels</span> <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">dragUp</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'.selector'</span><span class="token punctuation">,</span> <span class="token variable">$pixels</span> <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">dragDown</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'.selector'</span><span class="token punctuation">,</span> <span class="token variable">$pixels</span> <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>最后，你可以通过给定的偏移量拖动元素：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">dragOffset</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'.selector'</span><span class="token punctuation">,</span> <span class="token variable">$x</span> <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token variable">$y</span> <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="javascript-弹框" tabindex="-1"><a class="header-anchor" href="#javascript-弹框"><span>JavaScript 弹框</span></a></h3>
<p>Dusk 提供了多种方法来与 JavaScript 对话框交互。例如，你可以使用 <code v-pre>waitForDialog</code> 方法等待 JavaScript 对话框出现。此方法接受一个可选参数，指示等待对话框出现的秒数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">waitForDialog</span><span class="token punctuation">(</span><span class="token variable">$seconds</span> <span class="token operator">=</span> <span class="token constant">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><code v-pre>assertDialogOpened</code> 方法可用于断言对话框已显示并包含指定的消息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertDialogOpened</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Dialog message'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果 JavaScript 对话框包含一个提示，你可以使用 <code v-pre>typeInDialog</code> 方法在提示中输入一个值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">typeInDialog</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>要通过点击「确定」按钮关闭打开的 JavaScript 对话框，你可以调用 <code v-pre>acceptDialog</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">acceptDialog</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>要通过点击「取消」按钮关闭打开的 JavaScript 对话框，你可以调用 <code v-pre>dismissDialog</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">dismissDialog</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="与内嵌窗口交互" tabindex="-1"><a class="header-anchor" href="#与内嵌窗口交互"><span>与内嵌窗口交互</span></a></h3>
<p>如果你需要与 iframe 中的元素交互，你可以使用 <code v-pre>withinFrame</code> 方法。在提供给 <code v-pre>withinFrame</code> 方法的闭包中发生的所有元素交互都将限定在指定 iframe 的上下文中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">withinFrame</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'#credit-card-details'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$browser</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">type</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'input[name="cardnumber"]'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'4242424242424242'</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">type</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'input[name="exp-date"]'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'1224'</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">type</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'input[name="cvc"]'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'123'</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">press</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Pay'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="范围选择器" tabindex="-1"><a class="header-anchor" href="#范围选择器"><span>范围选择器</span></a></h3>
<p>有时你可能想在给定选择器的范围内执行多个操作。例如，你可能想要断言某些文本仅存在于表格中，然后在该表格中点击一个按钮。你可以使用 <code v-pre>with</code> 方法来实现这一点。在提供给 <code v-pre>with</code> 方法的闭包中执行的所有操作都将限定在原始选择器的范围内：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">with</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'.table'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Browser</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">assertSee</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">)</span></span>
<span class="line">          <span class="token operator">-></span><span class="token function">clickLink</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Delete'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可能偶尔需要在当前范围之外执行断言。你可以使用 <code v-pre>elsewhere</code> 和 <code v-pre>elsewhereWhenAvailable</code> 方法来实现这一点：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"> <span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">with</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'.table'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Browser</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// 当前的作用域是 `body .table`...</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">elsewhere</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'.page-title'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Browser</span> <span class="token variable">$title</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">// 当前的作用域是 `body .page-title`...</span></span>
<span class="line">        <span class="token variable">$title</span><span class="token operator">-></span><span class="token function">assertSee</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">elsewhereWhenAvailable</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'.page-title'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Browser</span> <span class="token variable">$title</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">// 当前的作用域是 `body .page-title`...</span></span>
<span class="line">        <span class="token variable">$title</span><span class="token operator">-></span><span class="token function">assertSee</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="等待元素" tabindex="-1"><a class="header-anchor" href="#等待元素"><span>等待元素</span></a></h3>
<p>在测试大量使用 JavaScript 的应用程序时，通常需要在继续进行测试之前「等待」某些元素或数据变得可用。Dusk 使这变得非常简单。使用多种方法，你可以等待页面上的元素变得可见，甚至等待给定的 JavaScript 表达式评估为 <code v-pre>true</code>。</p>
<h4 id="等待" tabindex="-1"><a class="header-anchor" href="#等待"><span>等待</span></a></h4>
<p>如果你只需要暂停测试一段时间（以毫秒为单位），请使用 <code v-pre>pause</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">pause</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果你只需要在给定条件为 <code v-pre>true</code> 时暂停测试，请使用 <code v-pre>pauseIf</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">pauseIf</span><span class="token punctuation">(</span><span class="token class-name static-context">App</span><span class="token operator">::</span><span class="token function">environment</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'production'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>同样，如果你需要在给定条件不为 <code v-pre>true</code> 时暂停测试，可以使用 <code v-pre>pauseUnless</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">pauseUnless</span><span class="token punctuation">(</span><span class="token class-name static-context">App</span><span class="token operator">::</span><span class="token function">environment</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'testing'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="等待选择器" tabindex="-1"><a class="header-anchor" href="#等待选择器"><span>等待选择器</span></a></h4>
<p><code v-pre>waitFor</code> 方法可用于暂停测试的执行，直到与给定 CSS 或 Dusk 选择器匹配的元素显示在页面上。默认情况下，这将在抛出异常之前暂停测试最多五秒钟。如果需要，你可以将自定义超时阈值作为方法的第二个参数传递：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 等待选择器最多 5 秒...</span></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">waitFor</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'.selector'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 等待选择器最多 1 秒...</span></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">waitFor</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'.selector'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以等待直到与指定选择器匹配的元素包含给定的文本：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 最多等待 5 秒，让选择器包含给定的文本...</span></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">waitForTextIn</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'.selector'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 最多等待 1 秒，让选择器包含给定的文本...</span></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">waitForTextIn</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'.selector'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以等待直到与给定选择器匹配的元素从页面上消失：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 等待最多 5 秒，直到选择器消失...</span></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">waitUntilMissing</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'.selector'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 等待最多 1 秒，直到选择器消失...</span></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">waitUntilMissing</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'.selector'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>或者，你可以等待与指定选择器匹配的元素被启用或禁用：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 最多等待 5 秒，直到选择器被启用...</span></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">waitUntilEnabled</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'.selector'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 最多等待 1 秒，直到选择器被启用...</span></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">waitUntilEnabled</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'.selector'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 最多等待 5 秒，直到选择器被禁用...</span></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">waitUntilDisabled</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'.selector'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 最多等待 1 秒，直到选择器被禁用...</span></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">waitUntilDisabled</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'.selector'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="选择器可用时限定作用域" tabindex="-1"><a class="header-anchor" href="#选择器可用时限定作用域"><span>选择器可用时限定作用域</span></a></h4>
<p>偶尔，你可能想等待与指定选择器匹配的元素出现，然后与该元素交互。例如，你可能想等待一个模态窗口变得可用，然后在模态窗口中点击「确定」按钮。<code v-pre>whenAvailable</code> 方法可用于实现这一点。在给定闭包中执行的所有元素操作都将限定在原始选择器的范围内：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">whenAvailable</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'.modal'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Browser</span> <span class="token variable">$modal</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$modal</span><span class="token operator">-></span><span class="token function">assertSee</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">)</span></span>
<span class="line">          <span class="token operator">-></span><span class="token function">press</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'OK'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="等待文本" tabindex="-1"><a class="header-anchor" href="#等待文本"><span>等待文本</span></a></h4>
<p><code v-pre>waitForText</code> 方法可用于等待指定文本显示在页面上：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 等待文本（显示）最多 5 秒...</span></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">waitForText</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 等待文本（显示）最多 1 秒...</span></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">waitForText</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以使用 <code v-pre>waitUntilMissingText</code> 方法等待显示的文本从页面上移除：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 最多等待 5 秒，直到文本被移除...</span></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">waitUntilMissingText</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 最多等待 1 秒，直到文本被移除...</span></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">waitUntilMissingText</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="等待链接" tabindex="-1"><a class="header-anchor" href="#等待链接"><span>等待链接</span></a></h4>
<p><code v-pre>waitForLink</code> 方法可用于等待指定链接文本显示在页面上：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 最多等待 5 秒，直到链接出现...</span></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">waitForLink</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Create'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 最多等待 1 秒，直到链接出现...</span></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">waitForLink</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Create'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="等待输入框" tabindex="-1"><a class="header-anchor" href="#等待输入框"><span>等待输入框</span></a></h4>
<p><code v-pre>waitForInput</code> 方法可用于等待指定输入字段显示在页面上：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 最多等待 5 秒，直到输入字段出现...</span></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">waitForInput</span><span class="token punctuation">(</span><span class="token variable">$field</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 最多等待 1 秒，直到输入字段出现...</span></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">waitForInput</span><span class="token punctuation">(</span><span class="token variable">$field</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="等待页面地址" tabindex="-1"><a class="header-anchor" href="#等待页面地址"><span>等待页面地址</span></a></h4>
<p>在做出路径断言时，例如 <code v-pre>$browser-&gt;assertPathIs('/home')</code>，如果 <code v-pre>window.location.pathname</code> 是异步更新的，断言可能会失败。你可以使用 <code v-pre>waitForLocation</code> 方法等待位置变为给定值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">waitForLocation</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/secret'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><code v-pre>waitForLocation</code> 方法也可以用于等待当前窗口地址变为完全限定的 URL：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">waitForLocation</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'https://example.com/path'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你也可以等待 <a href="https://learnku.com/docs/laravel/11.x/routing#named-routes" target="_blank" rel="noopener noreferrer">命名路由</a> 的地址：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">waitForRoute</span><span class="token punctuation">(</span><span class="token variable">$routeName</span><span class="token punctuation">,</span> <span class="token variable">$parameters</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="等待页面重新加载" tabindex="-1"><a class="header-anchor" href="#等待页面重新加载"><span>等待页面重新加载</span></a></h4>
<p>如果你需要在执行操作后等待页面重新加载，请使用 <code v-pre>waitForReload</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Dusk<span class="token punctuation">\</span>Browser</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">waitForReload</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Browser</span> <span class="token variable">$browser</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">press</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Submit'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token operator">-></span><span class="token function">assertSee</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Success!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>由于需要等待页面重新加载通常发生在点击按钮之后，你可以使用 <code v-pre>clickAndWaitForReload</code> 方法以方便地实现这一点：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">clickAndWaitForReload</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'.selector'</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">assertSee</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'something'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="等待-javascript-表达式" tabindex="-1"><a class="header-anchor" href="#等待-javascript-表达式"><span>等待 JavaScript 表达式</span></a></h4>
<p>有时你可能想要暂停测试的执行，直到给定的 JavaScript 表达式评估为 <code v-pre>true</code>。你可以使用 <code v-pre>waitUntil</code> 方法轻松实现这一点。当传递一个表达式给此方法时，你不需要包含 <code v-pre>return</code> 关键字或结尾的分号：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 最多等待 5 秒，直到表达式为真...</span></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">waitUntil</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'App.data.servers.length > 0'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 最多等待 1 秒，直到表达式为真...</span></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">waitUntil</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'App.data.servers.length > 0'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="等待-vue-表达式" tabindex="-1"><a class="header-anchor" href="#等待-vue-表达式"><span>等待 Vue 表达式</span></a></h4>
<p><code v-pre>waitUntilVue</code> 和 <code v-pre>waitUntilVueIsNot</code> 方法可用于等待 <a href="https://vuejs.org/" target="_blank" rel="noopener noreferrer">Vue 组件</a> 属性具有指定值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 等待直到组件属性包含给定值...</span></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">waitUntilVue</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user.name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'@user'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 等待直到组件属性不包含给定值...</span></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">waitUntilVueIsNot</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user.name'</span><span class="token punctuation">,</span> <span class="token constant">null</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'@user'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="等待-javascript-事件" tabindex="-1"><a class="header-anchor" href="#等待-javascript-事件"><span>等待 JavaScript 事件</span></a></h4>
<p><code v-pre>waitForEvent</code> 方法可用于暂停测试的执行，直到发生 JavaScript 事件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">waitForEvent</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'load'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>事件监听器附加到当前范围，默认情况下是 <code v-pre>body</code> 元素。当使用范围选择器时，事件监听器将附加到匹配的元素上：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">with</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'iframe'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Browser</span> <span class="token variable">$iframe</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// 等待 iframe 的加载事件...</span></span>
<span class="line">    <span class="token variable">$iframe</span><span class="token operator">-></span><span class="token function">waitForEvent</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'load'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以将选择器作为 <code v-pre>waitForEvent</code> 方法的第二个参数提供，以将事件监听器附加到特定元素上：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">waitForEvent</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'load'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'.selector'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你也可以等待 <code v-pre>document</code> 和 <code v-pre>window</code> 对象上的事件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 等待直到文档被滚动...</span></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">waitForEvent</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'scroll'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'document'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 最多等待 5 秒，直到窗口大小被调整...</span></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">waitForEvent</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'resize'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'window'</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="使用回调等待" tabindex="-1"><a class="header-anchor" href="#使用回调等待"><span>使用回调等待</span></a></h4>
<p>Dusk 中的许多「等待」方法都依赖于底层的 <code v-pre>waitUsing</code> 方法。你可以直接使用此方法来等待给定的闭包返回 <code v-pre>true</code>。<code v-pre>waitUsing</code> 方法接受等待的最大秒数、闭包应被评估的间隔、闭包以及一个可选的失败消息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">waitUsing</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">use</span> <span class="token punctuation">(</span><span class="token variable">$something</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$something</span><span class="token operator">-></span><span class="token function">isReady</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token string double-quoted-string">"Something wasn't ready in time."</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="滚动元素到视图中" tabindex="-1"><a class="header-anchor" href="#滚动元素到视图中"><span>滚动元素到视图中</span></a></h3>
<p>有时你可能无法点击一个元素，因为它在浏览器的可视区域之外。<code v-pre>scrollIntoView</code> 方法将滚动浏览器窗口，直到指定选择器处的元素位于视图中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">scrollIntoView</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'.selector'</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">click</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'.selector'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="可以的断言" tabindex="-1"><a class="header-anchor" href="#可以的断言"><span>可以的断言</span></a></h2>
<p>Dusk 提供了各种针对应用程序的断言。所有可用的断言都记录在下面的列表中：</p>
<h4 id="asserttitle" tabindex="-1"><a class="header-anchor" href="#asserttitle"><span>assertTitle</span></a></h4>
<p>断言页面标题与给定文本相同：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertTitle</span><span class="token punctuation">(</span><span class="token variable">$title</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="asserttitlecontains" tabindex="-1"><a class="header-anchor" href="#asserttitlecontains"><span>assertTitleContains</span></a></h4>
<p>断言页面标题包含给定文本：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertTitleContains</span><span class="token punctuation">(</span><span class="token variable">$title</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="asserturlis" tabindex="-1"><a class="header-anchor" href="#asserturlis"><span>assertUrlIs</span></a></h4>
<p>断言当前 URL（不包括查询字符串）与给定字符串相同：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertUrlIs</span><span class="token punctuation">(</span><span class="token variable">$url</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertschemeis" tabindex="-1"><a class="header-anchor" href="#assertschemeis"><span>assertSchemeIs</span></a></h4>
<p>断言当前 URL scheme 与给定 scheme 相同：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertSchemeIs</span><span class="token punctuation">(</span><span class="token variable">$scheme</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertschemeisnot" tabindex="-1"><a class="header-anchor" href="#assertschemeisnot"><span>assertSchemeIsNot</span></a></h4>
<p>断言当前 URL scheme 与给定 scheme 不同：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertSchemeIsNot</span><span class="token punctuation">(</span><span class="token variable">$scheme</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="asserthostis" tabindex="-1"><a class="header-anchor" href="#asserthostis"><span>assertHostIs</span></a></h4>
<p>断言当前 URL 主机与给定主机相同：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertHostIs</span><span class="token punctuation">(</span><span class="token variable">$host</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="asserthostisnot" tabindex="-1"><a class="header-anchor" href="#asserthostisnot"><span>assertHostIsNot</span></a></h4>
<p>断言当前 URL host 与给定 host 不同：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertHostIsNot</span><span class="token punctuation">(</span><span class="token variable">$host</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertportis" tabindex="-1"><a class="header-anchor" href="#assertportis"><span>assertPortIs</span></a></h4>
<p>断言当前 URL 端口与给定端口相同：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertPortIs</span><span class="token punctuation">(</span><span class="token variable">$port</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertportisnot" tabindex="-1"><a class="header-anchor" href="#assertportisnot"><span>assertPortIsNot</span></a></h4>
<p>断言当前 URL 端口与给定端口不同：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertPortIsNot</span><span class="token punctuation">(</span><span class="token variable">$port</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertpathbeginswith" tabindex="-1"><a class="header-anchor" href="#assertpathbeginswith"><span>assertPathBeginsWith</span></a></h4>
<p>断言当前 URL 路径以给定路径开头：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertPathBeginsWith</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/home'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertpathendswith" tabindex="-1"><a class="header-anchor" href="#assertpathendswith"><span>assertPathEndsWith</span></a></h4>
<p>断言当前 URL 路径以给定路径结尾：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertPathEndsWith</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/home'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertpathcontains" tabindex="-1"><a class="header-anchor" href="#assertpathcontains"><span>assertPathContains</span></a></h4>
<p>断言当前 URL 路径包含给定路径：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertPathContains</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/home'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertpathis" tabindex="-1"><a class="header-anchor" href="#assertpathis"><span>assertPathIs</span></a></h4>
<p>断言当前路径与给定路径相同：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertPathIs</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/home'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertpathisnot" tabindex="-1"><a class="header-anchor" href="#assertpathisnot"><span>assertPathIsNot</span></a></h4>
<p>断言当前路径与给定路径不同：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertPathIsNot</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/home'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertrouteis" tabindex="-1"><a class="header-anchor" href="#assertrouteis"><span>assertRouteIs</span></a></h4>
<p>断言当前 URL 与给定 <a href="https://learnku.com/docs/laravel/11.x/routing#named-routes" target="_blank" rel="noopener noreferrer">命名路由</a> 的 URL 相同：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertRouteIs</span><span class="token punctuation">(</span><span class="token variable">$name</span><span class="token punctuation">,</span> <span class="token variable">$parameters</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertquerystringhas" tabindex="-1"><a class="header-anchor" href="#assertquerystringhas"><span>assertQueryStringHas</span></a></h4>
<p>断言给定的查询字符串参数存在：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertQueryStringHas</span><span class="token punctuation">(</span><span class="token variable">$name</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>断言给定的查询字符串参数存在并具有给定值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertQueryStringHas</span><span class="token punctuation">(</span><span class="token variable">$name</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertquerystringmissing" tabindex="-1"><a class="header-anchor" href="#assertquerystringmissing"><span>assertQueryStringMissing</span></a></h4>
<p>断言给定的查询字符串参数缺失：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertQueryStringMissing</span><span class="token punctuation">(</span><span class="token variable">$name</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertfragmentis" tabindex="-1"><a class="header-anchor" href="#assertfragmentis"><span>assertFragmentIs</span></a></h4>
<p>断言 URL 的当前哈希片段与给定片段相同：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertFragmentIs</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'anchor'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertfragmentbeginswith" tabindex="-1"><a class="header-anchor" href="#assertfragmentbeginswith"><span>assertFragmentBeginsWith</span></a></h4>
<p>断言 URL 的当前哈希片段以给定片段开头：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertFragmentBeginsWith</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'anchor'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertfragmentisnot" tabindex="-1"><a class="header-anchor" href="#assertfragmentisnot"><span>assertFragmentIsNot</span></a></h4>
<p>断言 URL 的当前哈希片段与给定片段不同：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertFragmentIsNot</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'anchor'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="asserthascookie" tabindex="-1"><a class="header-anchor" href="#asserthascookie"><span>assertHasCookie</span></a></h4>
<p>断言给定的加密 cookie 存在：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertHasCookie</span><span class="token punctuation">(</span><span class="token variable">$name</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="asserthasplaincookie" tabindex="-1"><a class="header-anchor" href="#asserthasplaincookie"><span>assertHasPlainCookie</span></a></h4>
<p>断言给定的未加密 cookie 存在：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertHasPlainCookie</span><span class="token punctuation">(</span><span class="token variable">$name</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertcookiemissing" tabindex="-1"><a class="header-anchor" href="#assertcookiemissing"><span>assertCookieMissing</span></a></h4>
<p>断言给定的加密 cookie 不存在：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertCookieMissing</span><span class="token punctuation">(</span><span class="token variable">$name</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertplaincookiemissing" tabindex="-1"><a class="header-anchor" href="#assertplaincookiemissing"><span>assertPlainCookieMissing</span></a></h4>
<p>断言给定的未加密 cookie 不存在：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertPlainCookieMissing</span><span class="token punctuation">(</span><span class="token variable">$name</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertcookievalue" tabindex="-1"><a class="header-anchor" href="#assertcookievalue"><span>assertCookieValue</span></a></h4>
<p>断言加密 cookie 具有给定值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertCookieValue</span><span class="token punctuation">(</span><span class="token variable">$name</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertplaincookievalue" tabindex="-1"><a class="header-anchor" href="#assertplaincookievalue"><span>assertPlainCookieValue</span></a></h4>
<p>断言未加密 cookie 具有给定值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertPlainCookieValue</span><span class="token punctuation">(</span><span class="token variable">$name</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertsee" tabindex="-1"><a class="header-anchor" href="#assertsee"><span>assertSee</span></a></h4>
<p>断言给定文本存在于页面上：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertSee</span><span class="token punctuation">(</span><span class="token variable">$text</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertdontsee" tabindex="-1"><a class="header-anchor" href="#assertdontsee"><span>assertDontSee</span></a></h4>
<p>断言给定的文本不存在于页面上：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertDontSee</span><span class="token punctuation">(</span><span class="token variable">$text</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertseein" tabindex="-1"><a class="header-anchor" href="#assertseein"><span>assertSeeIn</span></a></h4>
<p>断言给定的文本存在于选择器中:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertSeeIn</span><span class="token punctuation">(</span><span class="token variable">$selector</span><span class="token punctuation">,</span> <span class="token variable">$text</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertdontseein" tabindex="-1"><a class="header-anchor" href="#assertdontseein"><span>assertDontSeeIn</span></a></h4>
<p>断言给定的文本不存在于选择器中:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertDontSeeIn</span><span class="token punctuation">(</span><span class="token variable">$selector</span><span class="token punctuation">,</span> <span class="token variable">$text</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertseeanythingin" tabindex="-1"><a class="header-anchor" href="#assertseeanythingin"><span>assertSeeAnythingIn</span></a></h4>
<p>断言选择器中存在任何文本:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertSeeAnythingIn</span><span class="token punctuation">(</span><span class="token variable">$selector</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertseenothingin" tabindex="-1"><a class="header-anchor" href="#assertseenothingin"><span>assertSeeNothingIn</span></a></h4>
<p>断言选择器中没有文本:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertSeeNothingIn</span><span class="token punctuation">(</span><span class="token variable">$selector</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertscript" tabindex="-1"><a class="header-anchor" href="#assertscript"><span>assertScript</span></a></h4>
<p>断言给定 JavaScript 表达式的计算结果为给定值:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertScript</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'window.isLoaded'</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">assertScript</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'document.readyState'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'complete'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="assertsourcehas" tabindex="-1"><a class="header-anchor" href="#assertsourcehas"><span>assertSourceHas</span></a></h4>
<p>断言给定的源代码存在于页面上:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertSourceHas</span><span class="token punctuation">(</span><span class="token variable">$code</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertsourcemissing" tabindex="-1"><a class="header-anchor" href="#assertsourcemissing"><span>assertSourceMissing</span></a></h4>
<p>断言给定的源代码不存在于页面上:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertSourceMissing</span><span class="token punctuation">(</span><span class="token variable">$code</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertseelink" tabindex="-1"><a class="header-anchor" href="#assertseelink"><span>assertSeeLink</span></a></h4>
<p>断言给定的链接存在于页面上:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertSeeLink</span><span class="token punctuation">(</span><span class="token variable">$linkText</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertdontseelink" tabindex="-1"><a class="header-anchor" href="#assertdontseelink"><span>assertDontSeeLink</span></a></h4>
<p>断言给定的链接不存在于页面上:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertDontSeeLink</span><span class="token punctuation">(</span><span class="token variable">$linkText</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertinputvalue" tabindex="-1"><a class="header-anchor" href="#assertinputvalue"><span>assertInputValue</span></a></h4>
<p>断言给定的输入字段具有给定的值:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertInputValue</span><span class="token punctuation">(</span><span class="token variable">$field</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertinputvalueisnot" tabindex="-1"><a class="header-anchor" href="#assertinputvalueisnot"><span>assertInputValueIsNot</span></a></h4>
<p>断言给定的输入字段没有给定的值:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertInputValueIsNot</span><span class="token punctuation">(</span><span class="token variable">$field</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertchecked" tabindex="-1"><a class="header-anchor" href="#assertchecked"><span>assertChecked</span></a></h4>
<p>断言给定的复选框被选中:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertChecked</span><span class="token punctuation">(</span><span class="token variable">$field</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertnotchecked" tabindex="-1"><a class="header-anchor" href="#assertnotchecked"><span>assertNotChecked</span></a></h4>
<p>断言给定的复选框未被选中:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertNotChecked</span><span class="token punctuation">(</span><span class="token variable">$field</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertindeterminate" tabindex="-1"><a class="header-anchor" href="#assertindeterminate"><span>assertIndeterminate</span></a></h4>
<p>断言给定的复选框处于不确定状态:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertIndeterminate</span><span class="token punctuation">(</span><span class="token variable">$field</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertradioselected" tabindex="-1"><a class="header-anchor" href="#assertradioselected"><span>assertRadioSelected</span></a></h4>
<p>断言给定的单选字段已被选中:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertRadioSelected</span><span class="token punctuation">(</span><span class="token variable">$field</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertradionotselected" tabindex="-1"><a class="header-anchor" href="#assertradionotselected"><span>assertRadioNotSelected</span></a></h4>
<p>断言给定的单选字段未被选中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertRadioNotSelected</span><span class="token punctuation">(</span><span class="token variable">$field</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertselected" tabindex="-1"><a class="header-anchor" href="#assertselected"><span>assertSelected</span></a></h4>
<p>断言给定的下拉框已经选中了给定的值:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertSelected</span><span class="token punctuation">(</span><span class="token variable">$field</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertnotselected" tabindex="-1"><a class="header-anchor" href="#assertnotselected"><span>assertNotSelected</span></a></h4>
<p>断言给定的下拉列表没有选中给定的值:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertNotSelected</span><span class="token punctuation">(</span><span class="token variable">$field</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertselecthasoptions" tabindex="-1"><a class="header-anchor" href="#assertselecthasoptions"><span>assertSelectHasOptions</span></a></h4>
<p>断言给定的值数组可供选择：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertSelectHasOptions</span><span class="token punctuation">(</span><span class="token variable">$field</span><span class="token punctuation">,</span> <span class="token variable">$values</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertselectmissingoptions" tabindex="-1"><a class="header-anchor" href="#assertselectmissingoptions"><span>assertSelectMissingOptions</span></a></h4>
<p>断言给定的值数组不可选:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertSelectMissingOptions</span><span class="token punctuation">(</span><span class="token variable">$field</span><span class="token punctuation">,</span> <span class="token variable">$values</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertselecthasoption" tabindex="-1"><a class="header-anchor" href="#assertselecthasoption"><span>assertSelectHasOption</span></a></h4>
<p>断言给定的值可以在给定的字段上选择:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertSelectHasOption</span><span class="token punctuation">(</span><span class="token variable">$field</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertselectmissingoption" tabindex="-1"><a class="header-anchor" href="#assertselectmissingoption"><span>assertSelectMissingOption</span></a></h4>
<p>断言给定的值不可选:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertSelectMissingOption</span><span class="token punctuation">(</span><span class="token variable">$field</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertvalue" tabindex="-1"><a class="header-anchor" href="#assertvalue"><span>assertValue</span></a></h4>
<p>断言与给定选择器匹配的元素具有给定的值:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertValue</span><span class="token punctuation">(</span><span class="token variable">$selector</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertvalueisnot" tabindex="-1"><a class="header-anchor" href="#assertvalueisnot"><span>assertValueIsNot</span></a></h4>
<p>断言与给定选择器匹配的元素不具有给定的值:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertValueIsNot</span><span class="token punctuation">(</span><span class="token variable">$selector</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertattribute" tabindex="-1"><a class="header-anchor" href="#assertattribute"><span>assertAttribute</span></a></h4>
<p>断言与给定选择器匹配的元素在提供的属性中具有指定的值:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertAttribute</span><span class="token punctuation">(</span><span class="token variable">$selector</span><span class="token punctuation">,</span> <span class="token variable">$attribute</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertattributecontains" tabindex="-1"><a class="header-anchor" href="#assertattributecontains"><span>assertAttributeContains</span></a></h4>
<p>断言与给定选择器匹配的元素在提供的属性中包含指定的值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertAttributeContains</span><span class="token punctuation">(</span><span class="token variable">$selector</span><span class="token punctuation">,</span> <span class="token variable">$attribute</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertattributedoesntcontain" tabindex="-1"><a class="header-anchor" href="#assertattributedoesntcontain"><span>assertAttributeDoesntContain</span></a></h4>
<p>断言与给定选择器匹配的元素在提供的属性中不包含指定的值:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertAttributeDoesntContain</span><span class="token punctuation">(</span><span class="token variable">$selector</span><span class="token punctuation">,</span> <span class="token variable">$attribute</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertariaattribute" tabindex="-1"><a class="header-anchor" href="#assertariaattribute"><span>assertAriaAttribute</span></a></h4>
<p>断言与给定选择器匹配的元素在提供的 aria 属性中具有指定的值:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertAriaAttribute</span><span class="token punctuation">(</span><span class="token variable">$selector</span><span class="token punctuation">,</span> <span class="token variable">$attribute</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>例如，给定标记 <code v-pre>&lt;button aria-label=&quot;Add&quot;&gt;&lt;/button&gt;</code>，你可以这样对 <code v-pre>aria-label</code> 属性进行断言：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertAriaAttribute</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'button'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'label'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Add'</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertdataattribute" tabindex="-1"><a class="header-anchor" href="#assertdataattribute"><span>assertDataAttribute</span></a></h4>
<p>断言与给定选择器匹配的元素在提供的 data 属性中具有给定值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertDataAttribute</span><span class="token punctuation">(</span><span class="token variable">$selector</span><span class="token punctuation">,</span> <span class="token variable">$attribute</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>例如，给定标记 <code v-pre>&lt;tr id=&quot;row-1&quot; data-content=&quot;attendees&quot;&gt;&lt;/tr&gt;</code>，你可以这样对 <code v-pre>data-label</code> 属性进行断言：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertDataAttribute</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'#row-1'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'content'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'attendees'</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertvisible" tabindex="-1"><a class="header-anchor" href="#assertvisible"><span>assertVisible</span></a></h4>
<p>断言与给定选择器匹配的元素是可见的:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertVisible</span><span class="token punctuation">(</span><span class="token variable">$selector</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertpresent" tabindex="-1"><a class="header-anchor" href="#assertpresent"><span>assertPresent</span></a></h4>
<p>断言与给定选择器匹配的元素在源代码中存在:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertPresent</span><span class="token punctuation">(</span><span class="token variable">$selector</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertnotpresent" tabindex="-1"><a class="header-anchor" href="#assertnotpresent"><span>assertNotPresent</span></a></h4>
<p>断言与给定选择器匹配的元素在源代码中不存在:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertNotPresent</span><span class="token punctuation">(</span><span class="token variable">$selector</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertmissing" tabindex="-1"><a class="header-anchor" href="#assertmissing"><span>assertMissing</span></a></h4>
<p>断言与给定选择器匹配的元素不可见:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertMissing</span><span class="token punctuation">(</span><span class="token variable">$selector</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertinputpresent" tabindex="-1"><a class="header-anchor" href="#assertinputpresent"><span>assertInputPresent</span></a></h4>
<p>断言具有给定名称的输入字段存在：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertInputPresent</span><span class="token punctuation">(</span><span class="token variable">$name</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertinputmissing" tabindex="-1"><a class="header-anchor" href="#assertinputmissing"><span>assertInputMissing</span></a></h4>
<p>断言具有给定名称的输入字段不存在于源代码中:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertInputMissing</span><span class="token punctuation">(</span><span class="token variable">$name</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertdialogopened" tabindex="-1"><a class="header-anchor" href="#assertdialogopened"><span>assertDialogOpened</span></a></h4>
<p>断言一个带有给定消息的 JavaScript 对话框已经打开:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertDialogOpened</span><span class="token punctuation">(</span><span class="token variable">$message</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertenabled" tabindex="-1"><a class="header-anchor" href="#assertenabled"><span>assertEnabled</span></a></h4>
<p>断言给定字段已启用:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertEnabled</span><span class="token punctuation">(</span><span class="token variable">$field</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertdisabled" tabindex="-1"><a class="header-anchor" href="#assertdisabled"><span>assertDisabled</span></a></h4>
<p>断言给定的字段是禁用的:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertDisabled</span><span class="token punctuation">(</span><span class="token variable">$field</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertbuttonenabled" tabindex="-1"><a class="header-anchor" href="#assertbuttonenabled"><span>assertButtonEnabled</span></a></h4>
<p>断言给定的按钮是启用的:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertButtonEnabled</span><span class="token punctuation">(</span><span class="token variable">$button</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertbuttondisabled" tabindex="-1"><a class="header-anchor" href="#assertbuttondisabled"><span>assertButtonDisabled</span></a></h4>
<p>断言给定的按钮被禁用:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertButtonDisabled</span><span class="token punctuation">(</span><span class="token variable">$button</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertfocused" tabindex="-1"><a class="header-anchor" href="#assertfocused"><span>assertFocused</span></a></h4>
<p>断言给定字段被聚焦：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertFocused</span><span class="token punctuation">(</span><span class="token variable">$field</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertnotfocused" tabindex="-1"><a class="header-anchor" href="#assertnotfocused"><span>assertNotFocused</span></a></h4>
<p>断言给定的字段没有聚焦:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertNotFocused</span><span class="token punctuation">(</span><span class="token variable">$field</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertauthenticated" tabindex="-1"><a class="header-anchor" href="#assertauthenticated"><span>assertAuthenticated</span></a></h4>
<p>断言用户已身份认证:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertAuthenticated</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertguest" tabindex="-1"><a class="header-anchor" href="#assertguest"><span>assertGuest</span></a></h4>
<p>断言用户没有身份认证:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertGuest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertauthenticatedas" tabindex="-1"><a class="header-anchor" href="#assertauthenticatedas"><span>assertAuthenticatedAs</span></a></h4>
<p>断言用户以给定用户身份进行的认证：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertAuthenticatedAs</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertvue" tabindex="-1"><a class="header-anchor" href="#assertvue"><span>assertVue</span></a></h4>
<p>Dusk 甚至允许你对 <a href="https://vuejs.org/" target="_blank" rel="noopener noreferrer">Vue 组件</a> 的状态进行断言。例如，假设你的应用程序包含以下 Vue 组件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// HTML...</span></span>
<span class="line"></span>
<span class="line"><span class="token operator">&lt;</span>profile dusk<span class="token operator">=</span><span class="token string double-quoted-string">"profile-component"</span><span class="token operator">></span><span class="token operator">&lt;</span><span class="token operator">/</span>profile<span class="token operator">></span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 组件定义...</span></span>
<span class="line"></span>
<span class="line">Vue<span class="token operator">.</span><span class="token function">component</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'profile'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span></span>
<span class="line">    template<span class="token punctuation">:</span> <span class="token string single-quoted-string">'&lt;div>{{ user.name }}&lt;/div>'</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">    <span class="token argument-name">data</span><span class="token punctuation">:</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token punctuation">{</span></span>
<span class="line">            user<span class="token punctuation">:</span> <span class="token punctuation">{</span></span>
<span class="line">                name<span class="token punctuation">:</span> <span class="token string single-quoted-string">'Taylor'</span></span>
<span class="line">            <span class="token punctuation">}</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以对 Vue 组件的状态进行这样的断言：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// Pest</span></span>
<span class="line"><span class="token function">test</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'vue'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">browse</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Browser</span> <span class="token variable">$browser</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">visit</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">assertVue</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user.name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'@profile-component'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// PHPUnit</span></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 一个基本的 Vue 测试示例。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">test_vue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">browse</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Browser</span> <span class="token variable">$browser</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">visit</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">assertVue</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user.name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'@profile-component'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="assertvueisnot" tabindex="-1"><a class="header-anchor" href="#assertvueisnot"><span>assertVueIsNot</span></a></h4>
<p>断言给定的 Vue 组件数据属性与给定值不同：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertVueIsNot</span><span class="token punctuation">(</span><span class="token variable">$property</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token variable">$componentSelector</span> <span class="token operator">=</span> <span class="token constant">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertvuecontains" tabindex="-1"><a class="header-anchor" href="#assertvuecontains"><span>assertVueContains</span></a></h4>
<p>断言给定的 Vue 组件数据属性是一个数组并包含给定值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertVueContains</span><span class="token punctuation">(</span><span class="token variable">$property</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token variable">$componentSelector</span> <span class="token operator">=</span> <span class="token constant">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="assertvuedoesntcontain" tabindex="-1"><a class="header-anchor" href="#assertvuedoesntcontain"><span>assertVueDoesntContain</span></a></h4>
<p>断言给定的 Vue 组件数据属性是一个数组并且不包含给定值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertVueDoesntContain</span><span class="token punctuation">(</span><span class="token variable">$property</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token variable">$componentSelector</span> <span class="token operator">=</span> <span class="token constant">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="页面" tabindex="-1"><a class="header-anchor" href="#页面"><span>页面</span></a></h2>
<p>有时，测试需要按顺序执行多个复杂的操作。这会使你的测试更难阅读和理解。Dusk 页面允许你定义可以在给定页面上通过单个方法执行的表达性操作。页面还允许你为应用程序或单个页面定义常用选择器的快捷方式。</p>
<h3 id="生成页面" tabindex="-1"><a class="header-anchor" href="#生成页面"><span>生成页面</span></a></h3>
<p>要生成一个页面对象，请执行 <code v-pre>dusk:page</code> Artisan 命令。所有页面对象都将放置在你的应用程序的 <code v-pre>tests/Browser/Pages</code> 目录中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan dusk<span class="token punctuation">:</span>page Login</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="配置页面" tabindex="-1"><a class="header-anchor" href="#配置页面"><span>配置页面</span></a></h3>
<p>默认情况下，页面有三个方法：<code v-pre>url</code>、<code v-pre>assert</code> 和 <code v-pre>elements</code>。我们现在将讨论 <code v-pre>url</code> 和 <code v-pre>assert</code> 方法。<code v-pre>elements</code> 方法将在 <a href="#shorthand-selectors">下面更详细地讨论</a>。</p>
<h4 id="url-方法" tabindex="-1"><a class="header-anchor" href="#url-方法"><span><code v-pre>url</code> 方法</span></a></h4>
<p><code v-pre>url</code> 方法应返回表示页面的 URL 的路径。Dusk 将在浏览器中导航到该页面时使用此 URL：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取页面的 URL。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">url</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">string</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token string single-quoted-string">'/login'</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="assert-方法" tabindex="-1"><a class="header-anchor" href="#assert-方法"><span><code v-pre>assert</code> 方法</span></a></h4>
<p><code v-pre>assert</code> 方法可以进行任何必要的断言，以验证浏览器实际上位于给定页面上。实际上不需要在此方法中放置任何内容；但是，如果你愿意，可以自由进行这些断言。导航到页面时，这些断言将自动运行：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 断言浏览器位于页面上。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">assert</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Browser</span> <span class="token variable">$browser</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertPathIs</span><span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">url</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="导航到页面" tabindex="-1"><a class="header-anchor" href="#导航到页面"><span>导航到页面</span></a></h3>
<p>页面一旦确定，你就可以使用 <code v-pre>visit</code> 方法导航到该页面：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Tests<span class="token punctuation">\</span>Browser<span class="token punctuation">\</span>Pages<span class="token punctuation">\</span>Login</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">visit</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Login</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>有时你可能已经位于给定页面上，并且需要「加载」页面的选择器和方法到当前测试上下文中。这在按下按钮并被重定向到给定页面而没有显式导航到该页面时很常见。在这种情况下，你可以使用 <code v-pre>on</code> 方法来加载页面：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Tests<span class="token punctuation">\</span>Browser<span class="token punctuation">\</span>Pages<span class="token punctuation">\</span>CreatePlaylist</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">visit</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/dashboard'</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">clickLink</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Create Playlist'</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">on</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">CreatePlaylist</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">assertSee</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'@create'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="快捷选择器" tabindex="-1"><a class="header-anchor" href="#快捷选择器"><span>快捷选择器</span></a></h3>
<p>页面类中的 <code v-pre>elements</code> 方法允许你为页面上的任何 CSS 选择器定义快速、易于记忆的快捷方式。例如，让我们为应用程序登录页面的「email」输入字段定义一个快捷方式：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取页面的元素快捷方式。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span>&lt;string, string></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">elements</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">array</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'@email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'input[name=email]'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>一旦定义好快捷方式，你就可以在任何通常使用完整 CSS 选择器的地方使用简写选择器：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">type</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'@email'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'taylor@laravel.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="全局快捷选择器" tabindex="-1"><a class="header-anchor" href="#全局快捷选择器"><span>全局快捷选择器</span></a></h4>
<p>安装 Dusk 后，一个基础 <code v-pre>Page</code> 类将放置在你的 <code v-pre>tests/Browser/Pages</code> 目录中。这个类包含一个 <code v-pre>siteElements</code> 方法，可用于定义应在应用程序的每个页面上可用的全局简写选择器：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取站点的全局元素快捷方式。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span>&lt;string, string></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">function</span> <span class="token function-definition function">siteElements</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">array</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'@element'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'#selector'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="页面方法" tabindex="-1"><a class="header-anchor" href="#页面方法"><span>页面方法</span></a></h3>
<p>除了页面上定义的默认方法外，你还可以定义其他方法，这些方法可以在你的测试中使用。例如，假设我们正在构建一个音乐管理应用程序。应用程序的一个页面的常见操作可能是创建播放列表。你可以在页面类上定义一个 <code v-pre>createPlaylist</code> 方法，而不是在每个测试中重写创建播放列表的逻辑：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">Tests<span class="token punctuation">\</span>Browser<span class="token punctuation">\</span>Pages</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Dusk<span class="token punctuation">\</span>Browser</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Dusk<span class="token punctuation">\</span>Page</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">Dashboard</span> <span class="token keyword">extends</span> <span class="token class-name">Page</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// 其他页面方法...</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 创建一个新的播放列表。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">createPlaylist</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Browser</span> <span class="token variable">$browser</span><span class="token punctuation">,</span> <span class="token keyword type-hint">string</span> <span class="token variable">$name</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">type</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token variable">$name</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">check</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'share'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">press</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Create Playlist'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>一旦定义好方法，你就可以在任何使用该页面的测试中使用它。浏览器实例将自动作为第一个参数传递给自定义页面方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Tests<span class="token punctuation">\</span>Browser<span class="token punctuation">\</span>Pages<span class="token punctuation">\</span>Dashboard</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">visit</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Dashboard</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">createPlaylist</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'My Playlist'</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">assertSee</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'My Playlist'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="组件" tabindex="-1"><a class="header-anchor" href="#组件"><span>组件</span></a></h2>
<p>组件类似于 Dusk 的「页面对象」，但旨在用于在整个应用程序中重用的 UI 和功能部分，例如导航栏或通知窗口。因此，组件不绑定到特定 URL。</p>
<h3 id="生成组件" tabindex="-1"><a class="header-anchor" href="#生成组件"><span>生成组件</span></a></h3>
<p>要生成一个组件，请执行 <code v-pre>dusk:component</code> Artisan 命令。新组件放置在 <code v-pre>tests/Browser/Components</code> 目录中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan dusk<span class="token punctuation">:</span>component DatePicker</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如上所示，「日期选择器」是一个可能在应用程序的多种页面上存在的组件示例。在测试套件中的数十个测试中手动编写浏览器自动化逻辑来选择日期可能会变得繁琐。相反，我们可以定义一个 Dusk 组件来表示日期选择器，从而将该逻辑封装在组件中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">Tests<span class="token punctuation">\</span>Browser<span class="token punctuation">\</span>Components</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Dusk<span class="token punctuation">\</span>Browser</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Dusk<span class="token punctuation">\</span>Component</span> <span class="token keyword">as</span> BaseComponent<span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">DatePicker</span> <span class="token keyword">extends</span> <span class="token class-name">BaseComponent</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 获取组件的根选择器。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">selector</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">string</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token string single-quoted-string">'.date-picker'</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 断言浏览器页面包含该组件。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">assert</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Browser</span> <span class="token variable">$browser</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">assertVisible</span><span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">selector</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 获取组件的元素快捷方式。</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span>&lt;string, string></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">elements</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">array</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'@date-field'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'input.datepicker-input'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'@year-list'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'div > div.datepicker-years'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'@month-list'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'div > div.datepicker-months'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'@day-list'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'div > div.datepicker-days'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 选择给定的日期。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">selectDate</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Browser</span> <span class="token variable">$browser</span><span class="token punctuation">,</span> <span class="token keyword type-hint">int</span> <span class="token variable">$year</span><span class="token punctuation">,</span> <span class="token keyword type-hint">int</span> <span class="token variable">$month</span><span class="token punctuation">,</span> <span class="token keyword type-hint">int</span> <span class="token variable">$day</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">click</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'@date-field'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">within</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'@year-list'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Browser</span> <span class="token variable">$browser</span><span class="token punctuation">)</span> <span class="token keyword">use</span> <span class="token punctuation">(</span><span class="token variable">$year</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                    <span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">click</span><span class="token punctuation">(</span><span class="token variable">$year</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">                <span class="token punctuation">}</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">within</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'@month-list'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Browser</span> <span class="token variable">$browser</span><span class="token punctuation">)</span> <span class="token keyword">use</span> <span class="token punctuation">(</span><span class="token variable">$month</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                    <span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">click</span><span class="token punctuation">(</span><span class="token variable">$month</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">                <span class="token punctuation">}</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">within</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'@day-list'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Browser</span> <span class="token variable">$browser</span><span class="token punctuation">)</span> <span class="token keyword">use</span> <span class="token punctuation">(</span><span class="token variable">$day</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                    <span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">click</span><span class="token punctuation">(</span><span class="token variable">$day</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">                <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="使用组件" tabindex="-1"><a class="header-anchor" href="#使用组件"><span>使用组件</span></a></h3>
<p>组件一旦定义好后，我们就可以轻松地从任何测试中在日期选择器中选择日期。而且，如果选择日期所需的逻辑发生变化，我们只需要更新组件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">// Pest</span>
<span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Testing<span class="token punctuation">\</span>DatabaseMigrations</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Dusk<span class="token punctuation">\</span>Browser</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Tests<span class="token punctuation">\</span>Browser<span class="token punctuation">\</span>Components<span class="token punctuation">\</span>DatePicker</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">uses</span><span class="token punctuation">(</span><span class="token class-name static-context">DatabaseMigrations</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">test</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'basic example'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">browse</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Browser</span> <span class="token variable">$browser</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">visit</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">within</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">DatePicker</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Browser</span> <span class="token variable">$browser</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                    <span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">selectDate</span><span class="token punctuation">(</span><span class="token number">2019</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">30</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">                <span class="token punctuation">}</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">assertSee</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'January'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">// PHPUnit</span>
<span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">Tests<span class="token punctuation">\</span>Browser</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Testing<span class="token punctuation">\</span>DatabaseMigrations</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Dusk<span class="token punctuation">\</span>Browser</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Tests<span class="token punctuation">\</span>Browser<span class="token punctuation">\</span>Components<span class="token punctuation">\</span>DatePicker</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Tests<span class="token punctuation">\</span>DuskTestCase</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">ExampleTest</span> <span class="token keyword">extends</span> <span class="token class-name">DuskTestCase</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 一个基本的组件测试示例。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">test_basic_example</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">browse</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Browser</span> <span class="token variable">$browser</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">visit</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/'</span><span class="token punctuation">)</span></span>
<span class="line">                    <span class="token operator">-></span><span class="token function">within</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">DatePicker</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Browser</span> <span class="token variable">$browser</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                        <span class="token variable">$browser</span><span class="token operator">-></span><span class="token function">selectDate</span><span class="token punctuation">(</span><span class="token number">2019</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">30</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">                    <span class="token punctuation">}</span><span class="token punctuation">)</span></span>
<span class="line">                    <span class="token operator">-></span><span class="token function">assertSee</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'January'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="continuous-integration" tabindex="-1"><a class="header-anchor" href="#continuous-integration"><span>Continuous Integration</span></a></h2>
<blockquote>
<p>[!警告]<br>
大多数 Dusk 持续集成配置期望你的 Laravel 应用程序使用内置的 PHP 开发服务器在端口 8000 上提供服务。因此，在继续之前，你应该确保你的持续集成环境具有 <code v-pre>APP_URL</code> 环境变量值为 <code v-pre>http://127.0.0.1:8000</code>。</p>
</blockquote>
<h3 id="heroku-ci" tabindex="-1"><a class="header-anchor" href="#heroku-ci"><span>Heroku CI</span></a></h3>
<p>要在 <a href="https://www.heroku.com/continuous-integration" target="_blank" rel="noopener noreferrer">Heroku CI</a> 上运行 Dusk 测试，请将以下 Google Chrome 构建包和脚本添加到你的 Heroku <code v-pre>app.json</code> 文件中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">{</span></span>
<span class="line">  <span class="token string double-quoted-string">"environments"</span><span class="token punctuation">:</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token string double-quoted-string">"test"</span><span class="token punctuation">:</span> <span class="token punctuation">{</span></span>
<span class="line">      <span class="token string double-quoted-string">"buildpacks"</span><span class="token punctuation">:</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token punctuation">{</span> <span class="token string double-quoted-string">"url"</span><span class="token punctuation">:</span> <span class="token string double-quoted-string">"heroku/php"</span> <span class="token punctuation">}</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">{</span> <span class="token string double-quoted-string">"url"</span><span class="token punctuation">:</span> <span class="token string double-quoted-string">"https://github.com/heroku/heroku-buildpack-google-chrome"</span> <span class="token punctuation">}</span></span>
<span class="line">      <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">      <span class="token string double-quoted-string">"scripts"</span><span class="token punctuation">:</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token string double-quoted-string">"test-setup"</span><span class="token punctuation">:</span> <span class="token string double-quoted-string">"cp .env.testing .env"</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string double-quoted-string">"test"</span><span class="token punctuation">:</span> <span class="token string double-quoted-string">"nohup bash -c './vendor/laravel/dusk/bin/chromedriver-linux > /dev/null 2>&amp;1 &amp;' &amp;&amp; nohup bash -c 'php artisan serve --no-reload > /dev/null 2>&amp;1 &amp;' &amp;&amp; php artisan dusk"</span></span>
<span class="line">      <span class="token punctuation">}</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line">  <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="travis-ci" tabindex="-1"><a class="header-anchor" href="#travis-ci"><span>Travis CI</span></a></h3>
<p>要在 <a href="https://travis-ci.org/" target="_blank" rel="noopener noreferrer">Travis CI</a> 上运行你的 Dusk 测试，请使用以下 <code v-pre>.travis.yml</code> 配置。由于 Travis CI 不是一个图形环境，我们需要采取一些额外的步骤来启动 Chrome 浏览器。此外，我们将使用 <code v-pre>php artisan serve</code> 来启动 PHP 的内置 Web 服务器：</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line">language: php</span>
<span class="line"></span>
<span class="line">php:</span>
<span class="line">  - 7.3</span>
<span class="line"></span>
<span class="line">addons:</span>
<span class="line">  chrome: stable</span>
<span class="line"></span>
<span class="line">install:</span>
<span class="line">  - cp .env.testing .env</span>
<span class="line">  - travis_retry composer install --no-interaction --prefer-dist</span>
<span class="line">  - php artisan key:generate</span>
<span class="line">  - php artisan dusk:chrome-driver</span>
<span class="line"></span>
<span class="line">before_script:</span>
<span class="line">  <span class="token key attr-name">- google-chrome-stable --headless --disable-gpu --remote-debugging-port</span><span class="token punctuation">=</span><span class="token value attr-value">9222 http://localhost &amp;</span></span>
<span class="line">  - php artisan serve --no-reload &amp;</span>
<span class="line"></span>
<span class="line">script:</span>
<span class="line">  - php artisan dusk</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="github-actions" tabindex="-1"><a class="header-anchor" href="#github-actions"><span>GitHub Actions</span></a></h3>
<p>如果你使用 <a href="https://github.com/features/actions" target="_blank" rel="noopener noreferrer">GitHub Actions</a> 来运行你的 Dusk 测试，你可以使用以下配置文件作为起点。与 TravisCI 一样，我们将使用 <code v-pre>php artisan serve</code> 命令来启动 PHP 的内置 Web 服务器：</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line">name: CI</span>
<span class="line">on: [push]</span>
<span class="line">jobs:</span>
<span class="line"></span>
<span class="line">  dusk-php:</span>
<span class="line">    runs-on: ubuntu-latest</span>
<span class="line">    env:</span>
<span class="line">      APP_URL: "http://127.0.0.1:8000"</span>
<span class="line">      DB_USERNAME: root</span>
<span class="line">      DB_PASSWORD: root</span>
<span class="line">      MAIL_MAILER: log</span>
<span class="line">    steps:</span>
<span class="line">      - uses: actions/checkout@v4</span>
<span class="line">      - name: Prepare The Environment</span>
<span class="line">        run: cp .env.example .env</span>
<span class="line">      - name: Create Database</span>
<span class="line">        run: |</span>
<span class="line">          sudo systemctl start mysql</span>
<span class="line">          <span class="token key attr-name">mysql --user</span><span class="token punctuation">=</span><span class="token value attr-value">"<span class="token inner-value">root" --password="root" -e "CREATE DATABASE \`my-database\` character set UTF8mb4 collate utf8mb4_bin;</span>"</span></span>
<span class="line">      - name: Install Composer Dependencies</span>
<span class="line">        run: composer install --no-progress --prefer-dist --optimize-autoloader</span>
<span class="line">      - name: Generate Application Key</span>
<span class="line">        run: php artisan key:generate</span>
<span class="line">      - name: Upgrade Chrome Driver</span>
<span class="line">        run: php artisan dusk:chrome-driver --detect</span>
<span class="line">      - name: Start Chrome Driver</span>
<span class="line">        run: ./vendor/laravel/dusk/bin/chromedriver-linux &amp;</span>
<span class="line">      - name: Run Laravel Server</span>
<span class="line">        run: php artisan serve --no-reload &amp;</span>
<span class="line">      - name: Run Dusk Tests</span>
<span class="line">        run: php artisan dusk</span>
<span class="line">      - name: Upload Screenshots</span>
<span class="line">        if: failure()</span>
<span class="line">        uses: actions/upload-artifact@v2</span>
<span class="line">        with:</span>
<span class="line">          name: screenshots</span>
<span class="line">          path: tests/Browser/screenshots</span>
<span class="line">      - name: Upload Console Logs</span>
<span class="line">        if: failure()</span>
<span class="line">        uses: actions/upload-artifact@v2</span>
<span class="line">        with:</span>
<span class="line">          name: console</span>
<span class="line">          path: tests/Browser/console</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="chipper-ci" tabindex="-1"><a class="header-anchor" href="#chipper-ci"><span>Chipper CI</span></a></h3>
<p>如果你使用 <a href="https://chipperci.com/" target="_blank" rel="noopener noreferrer">Chipper CI</a> 来运行你的 Dusk 测试，你可以使用以下配置文件作为起点。我们将使用 PHP 的内置服务器来运行 Laravel，以便我们可以监听请求：</p>
<div class="language-ini line-numbers-mode" data-highlighter="prismjs" data-ext="ini" data-title="ini"><pre v-pre class="language-ini"><code><span class="line"><span class="token comment"># .chipperci.yml 文件</span></span>
<span class="line">version: 1</span>
<span class="line"></span>
<span class="line">environment:</span>
<span class="line">  php: 8.2</span>
<span class="line">  node: 16</span>
<span class="line"></span>
<span class="line"><span class="token comment"># 在构建环境中包含 Chrome</span></span>
<span class="line">services:</span>
<span class="line">  - dusk</span>
<span class="line"></span>
<span class="line"><span class="token comment"># 构建所有提交</span></span>
<span class="line">on:</span>
<span class="line">   push:</span>
<span class="line">      branches: .*</span>
<span class="line"></span>
<span class="line">pipeline:</span>
<span class="line">  - name: Setup</span>
<span class="line">    cmd: |</span>
<span class="line">      cp -v .env.example .env</span>
<span class="line">      composer install --no-interaction --prefer-dist --optimize-autoloader</span>
<span class="line">      php artisan key:generate</span>
<span class="line"></span>
<span class="line">      <span class="token comment"># 创建一个 dusk 环境文件，确保 APP_URL 使用 BUILD_HOST：</span></span>
<span class="line">      cp -v .env .env.dusk.ci</span>
<span class="line">      <span class="token key attr-name">sed -i "s@APP_URL</span><span class="token punctuation">=</span><span class="token value attr-value">.*@APP_URL=http://$BUILD_HOST:8000@g" .env.dusk.ci</span></span>
<span class="line"></span>
<span class="line">  - name: Compile Assets</span>
<span class="line">    cmd: |</span>
<span class="line">      npm ci --no-audit</span>
<span class="line">      npm run build</span>
<span class="line"></span>
<span class="line">  - name: Browser Tests</span>
<span class="line">    cmd: |</span>
<span class="line">      php -S [::0]:8000 -t public 2>server.log &amp;</span>
<span class="line">      sleep 2</span>
<span class="line">      php artisan dusk:chrome-driver $CHROME_DRIVER</span>
<span class="line">      <span class="token key attr-name">php artisan dusk --env</span><span class="token punctuation">=</span><span class="token value attr-value">ci</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>要了解更多关于在 Chipper CI 上运行 Dusk 测试的信息，包括如何使用数据库，请查阅 <a href="https://chipperci.com/docs/testing/laravel-dusk-new/" target="_blank" rel="noopener noreferrer">官方 Chipper CI 文档</a>。</p>
<blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/11.x/duskmd/16712" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/du...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/11.x/duskmd/16712" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/du...</a></p>
</blockquote>
</div></template>


